blob: 6b51d8a848eab2b6cb0089f197ff0703ad5246b0 [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>
22#endif
23
pxinwr3405e052020-08-07 13:21:52 +080024#ifdef __VXWORKS__
25# include "pycore_bitutils.h" // _Py_popcount32()
26#endif
Victor Stinner4a21e572020-04-15 02:35:41 +020027#include "pycore_ceval.h" // _PyEval_ReInitThreads()
28#include "pycore_import.h" // _PyImport_ReInitLock()
Victor Stinner26881c82020-06-02 15:51:37 +020029#include "pycore_initconfig.h" // _PyStatus_EXCEPTION()
Victor Stinner4a21e572020-04-15 02:35:41 +020030#include "pycore_pystate.h" // _PyInterpreterState_GET()
31#include "structmember.h" // PyMemberDef
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020032#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020033# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010034#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020035# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020036#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000037
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020038/* On android API level 21, 'AT_EACCESS' is not declared although
39 * HAVE_FACCESSAT is defined. */
40#ifdef __ANDROID__
Victor Stinner5eca75d2020-04-15 15:07:31 +020041# undef HAVE_FACCESSAT
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020042#endif
43
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000044#include <stdio.h> /* needed for ctermid() */
45
Ronald Oussoren41761932020-11-08 10:05:27 +010046/*
47 * A number of APIs are available on macOS from a certain macOS version.
48 * To support building with a new SDK while deploying to older versions
49 * the availability test is split into two:
50 * - HAVE_<FUNCTION>: The configure check for compile time availability
51 * - HAVE_<FUNCTION>_RUNTIME: Runtime check for availability
52 *
53 * The latter is always true when not on macOS, or when using a compiler
54 * that does not support __has_builtin (older versions of Xcode).
55 *
56 * Due to compiler restrictions there is one valid use of HAVE_<FUNCTION>_RUNTIME:
57 * if (HAVE_<FUNCTION>_RUNTIME) { ... }
58 *
59 * In mixing the test with other tests or using negations will result in compile
60 * errors.
61 */
62#if defined(__APPLE__)
63
64#if defined(__has_builtin) && __has_builtin(__builtin_available)
65# define HAVE_FSTATAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
66# define HAVE_FACCESSAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
67# define HAVE_FCHMODAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
68# define HAVE_FCHOWNAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
69# define HAVE_LINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
70# define HAVE_FDOPENDIR_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
71# define HAVE_MKDIRAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
72# define HAVE_RENAMEAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
73# define HAVE_UNLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
74# define HAVE_OPENAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
75# define HAVE_READLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
76# define HAVE_SYMLINKAT_RUNTIME __builtin_available(macOS 10.10, iOS 8.0, *)
77# define HAVE_FUTIMENS_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
78# define HAVE_UTIMENSAT_RUNTIME __builtin_available(macOS 10.13, iOS 11.0, tvOS 11.0, watchOS 4.0, *)
79# define HAVE_PWRITEV_RUNTIME __builtin_available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *)
80
81# define HAVE_POSIX_SPAWN_SETSID_RUNTIME __builtin_available(macOS 10.15, *)
82
83#else /* Xcode 8 or earlier */
84
85 /* __builtin_available is not present in these compilers, but
86 * some of the symbols might be weak linked (10.10 SDK or later
87 * deploying on 10.9.
88 *
89 * Fall back to the older style of availability checking for
90 * symbols introduced in macOS 10.10.
91 */
92
93# ifdef HAVE_FSTATAT
94# define HAVE_FSTATAT_RUNTIME (fstatat != NULL)
95# endif
96
97# ifdef HAVE_FACCESSAT
98# define HAVE_FACCESSAT_RUNTIME (faccessat != NULL)
99# endif
100
101# ifdef HAVE_FCHMODAT
102# define HAVE_FCHMODAT_RUNTIME (fchmodat != NULL)
103# endif
104
105# ifdef HAVE_FCHOWNAT
106# define HAVE_FCHOWNAT_RUNTIME (fchownat != NULL)
107# endif
108
109# ifdef HAVE_LINKAT
110# define HAVE_LINKAT_RUNTIME (linkat != NULL)
111# endif
112
113# ifdef HAVE_FDOPENDIR
114# define HAVE_FDOPENDIR_RUNTIME (fdopendir != NULL)
115# endif
116
117# ifdef HAVE_MKDIRAT
118# define HAVE_MKDIRAT_RUNTIME (mkdirat != NULL)
119# endif
120
121# ifdef HAVE_RENAMEAT
122# define HAVE_RENAMEAT_RUNTIME (renameat != NULL)
123# endif
124
125# ifdef HAVE_UNLINKAT
126# define HAVE_UNLINKAT_RUNTIME (unlinkat != NULL)
127# endif
128
129# ifdef HAVE_OPENAT
130# define HAVE_OPENAT_RUNTIME (openat != NULL)
131# endif
132
133# ifdef HAVE_READLINKAT
134# define HAVE_READLINKAT_RUNTIME (readlinkat != NULL)
135# endif
136
137# ifdef HAVE_SYMLINKAT
138# define HAVE_SYMLINKAT_RUNTIME (symlinkat != NULL)
139# endif
140
141#endif
142
143#ifdef HAVE_FUTIMESAT
144/* Some of the logic for weak linking depends on this assertion */
145# error "HAVE_FUTIMESAT unexpectedly defined"
146#endif
147
148#else
149# define HAVE_FSTATAT_RUNTIME 1
150# define HAVE_FACCESSAT_RUNTIME 1
151# define HAVE_FCHMODAT_RUNTIME 1
152# define HAVE_FCHOWNAT_RUNTIME 1
153# define HAVE_LINKAT_RUNTIME 1
154# define HAVE_FDOPENDIR_RUNTIME 1
155# define HAVE_MKDIRAT_RUNTIME 1
156# define HAVE_RENAMEAT_RUNTIME 1
157# define HAVE_UNLINKAT_RUNTIME 1
158# define HAVE_OPENAT_RUNTIME 1
159# define HAVE_READLINKAT_RUNTIME 1
160# define HAVE_SYMLINKAT_RUNTIME 1
161# define HAVE_FUTIMENS_RUNTIME 1
162# define HAVE_UTIMENSAT_RUNTIME 1
163# define HAVE_PWRITEV_RUNTIME 1
164#endif
165
166
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167#ifdef __cplusplus
168extern "C" {
169#endif
170
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000171PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +0000172"This module provides access to operating system functionality that is\n\
173standardized by the C Standard and the POSIX standard (a thinly\n\
174disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +0000175corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176
Martin v. Löwis0073f2e2002-11-21 23:52:35 +0000177
Ross Lagerwall4d076da2011-03-18 06:56:53 +0200178#ifdef HAVE_SYS_UIO_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200179# include <sys/uio.h>
Ross Lagerwall4d076da2011-03-18 06:56:53 +0200180#endif
181
Christian Heimes75b96182017-09-05 15:53:09 +0200182#ifdef HAVE_SYS_SYSMACROS_H
183/* GNU C Library: major(), minor(), makedev() */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200184# include <sys/sysmacros.h>
Christian Heimes75b96182017-09-05 15:53:09 +0200185#endif
186
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000187#ifdef HAVE_SYS_TYPES_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200188# include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000189#endif /* HAVE_SYS_TYPES_H */
190
191#ifdef HAVE_SYS_STAT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200192# include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000193#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000194
Guido van Rossum36bc6801995-06-14 22:54:23 +0000195#ifdef HAVE_SYS_WAIT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200196# include <sys/wait.h> // WNOHANG
Guido van Rossum36bc6801995-06-14 22:54:23 +0000197#endif
Benjamin Peterson5c0c3252019-11-05 21:58:31 -0800198#ifdef HAVE_LINUX_WAIT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200199# include <linux/wait.h> // P_PIDFD
Benjamin Peterson5c0c3252019-11-05 21:58:31 -0800200#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000201
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000202#ifdef HAVE_SIGNAL_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200203# include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000204#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +0000205
Guido van Rossumb6775db1994-08-01 11:34:53 +0000206#ifdef HAVE_FCNTL_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200207# include <fcntl.h>
208#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000209
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000210#ifdef HAVE_GRP_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200211# include <grp.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +0000212#endif
213
Barry Warsaw5676bd12003-01-07 20:57:09 +0000214#ifdef HAVE_SYSEXITS_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200215# include <sysexits.h>
216#endif
Barry Warsaw5676bd12003-01-07 20:57:09 +0000217
Anthony Baxter8a560de2004-10-13 15:30:56 +0000218#ifdef HAVE_SYS_LOADAVG_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200219# include <sys/loadavg.h>
Anthony Baxter8a560de2004-10-13 15:30:56 +0000220#endif
221
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000222#ifdef HAVE_SYS_SENDFILE_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200223# include <sys/sendfile.h>
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000224#endif
225
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200226#if defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200227# include <copyfile.h>
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200228#endif
229
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500230#ifdef HAVE_SCHED_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200231# include <sched.h>
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500232#endif
233
Pablo Galindoaac4d032019-05-31 19:39:47 +0100234#ifdef HAVE_COPY_FILE_RANGE
Victor Stinner5eca75d2020-04-15 15:07:31 +0200235# include <unistd.h>
Pablo Galindoaac4d032019-05-31 19:39:47 +0100236#endif
237
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500238#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200239# undef HAVE_SCHED_SETAFFINITY
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500240#endif
241
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200242#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200243# define USE_XATTRS
Benjamin Peterson9428d532011-09-14 11:45:52 -0400244#endif
245
246#ifdef USE_XATTRS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200247# include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400248#endif
249
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000250#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200251# ifdef HAVE_SYS_SOCKET_H
252# include <sys/socket.h>
253# endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000254#endif
255
Victor Stinner8b905bd2011-10-25 13:34:04 +0200256#ifdef HAVE_DLFCN_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200257# include <dlfcn.h>
Victor Stinner8b905bd2011-10-25 13:34:04 +0200258#endif
259
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200260#ifdef __hpux
Victor Stinner5eca75d2020-04-15 15:07:31 +0200261# include <sys/mpctl.h>
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200262#endif
263
264#if defined(__DragonFly__) || \
265 defined(__OpenBSD__) || \
266 defined(__FreeBSD__) || \
267 defined(__NetBSD__) || \
268 defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200269# include <sys/sysctl.h>
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200270#endif
271
Victor Stinner9b1f4742016-09-06 16:18:52 -0700272#ifdef HAVE_LINUX_RANDOM_H
273# include <linux/random.h>
274#endif
275#ifdef HAVE_GETRANDOM_SYSCALL
276# include <sys/syscall.h>
277#endif
278
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100279#if defined(MS_WINDOWS)
280# define TERMSIZE_USE_CONIO
281#elif defined(HAVE_SYS_IOCTL_H)
282# include <sys/ioctl.h>
283# if defined(HAVE_TERMIOS_H)
284# include <termios.h>
285# endif
286# if defined(TIOCGWINSZ)
287# define TERMSIZE_USE_IOCTL
288# endif
289#endif /* MS_WINDOWS */
290
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000291/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000292/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000293#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200294# define HAVE_OPENDIR 1
295# define HAVE_SYSTEM 1
296# include <process.h>
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000297#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200298# ifdef _MSC_VER
299 /* Microsoft compiler */
300# define HAVE_GETPPID 1
301# define HAVE_GETLOGIN 1
302# define HAVE_SPAWNV 1
303# define HAVE_EXECV 1
304# define HAVE_WSPAWNV 1
305# define HAVE_WEXECV 1
306# define HAVE_PIPE 1
307# define HAVE_SYSTEM 1
308# define HAVE_CWAIT 1
309# define HAVE_FSYNC 1
310# define fsync _commit
311# else
312 /* Unix functions that the configure script doesn't check for */
313# ifndef __VXWORKS__
314# define HAVE_EXECV 1
315# define HAVE_FORK 1
316# if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
317# define HAVE_FORK1 1
318# endif
319# endif
320# define HAVE_GETEGID 1
321# define HAVE_GETEUID 1
322# define HAVE_GETGID 1
323# define HAVE_GETPPID 1
324# define HAVE_GETUID 1
325# define HAVE_KILL 1
326# define HAVE_OPENDIR 1
327# define HAVE_PIPE 1
328# define HAVE_SYSTEM 1
329# define HAVE_WAIT 1
330# define HAVE_TTYNAME 1
331# endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000332#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000333
Eddie Elizondob3966632019-11-05 07:16:14 -0800334_Py_IDENTIFIER(__fspath__);
Victor Stinnera2f7c002012-02-08 03:36:25 +0100335
Larry Hastings61272b72014-01-07 12:41:53 -0800336/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000337# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800338module os
Larry Hastings61272b72014-01-07 12:41:53 -0800339[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000340/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100341
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000342#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000343
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000344#if defined(__sgi)&&_COMPILER_VERSION>=700
345/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
346 (default) */
347extern char *ctermid_r(char *);
348#endif
349
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000350#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000351
pxinwrf2d7ac72019-05-21 18:46:37 +0800352#if defined(__VXWORKS__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200353# include <vxCpuLib.h>
354# include <rtpLib.h>
355# include <wait.h>
356# include <taskLib.h>
357# ifndef _P_WAIT
358# define _P_WAIT 0
359# define _P_NOWAIT 1
360# define _P_NOWAITO 1
361# endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800362#endif /* __VXWORKS__ */
363
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000364#ifdef HAVE_POSIX_SPAWN
Victor Stinner5eca75d2020-04-15 15:07:31 +0200365# include <spawn.h>
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000366#endif
367
Guido van Rossumb6775db1994-08-01 11:34:53 +0000368#ifdef HAVE_UTIME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200369# include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000370#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000371
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000372#ifdef HAVE_SYS_UTIME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200373# include <sys/utime.h>
374# define HAVE_UTIME_H /* pretend we do for the rest of this file */
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000375#endif /* HAVE_SYS_UTIME_H */
376
Guido van Rossumb6775db1994-08-01 11:34:53 +0000377#ifdef HAVE_SYS_TIMES_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200378# include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000379#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000380
381#ifdef HAVE_SYS_PARAM_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200382# include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000383#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000384
385#ifdef HAVE_SYS_UTSNAME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200386# include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000387#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000388
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000389#ifdef HAVE_DIRENT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200390# include <dirent.h>
391# define NAMLEN(dirent) strlen((dirent)->d_name)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000392#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200393# if defined(__WATCOMC__) && !defined(__QNX__)
394# include <direct.h>
395# define NAMLEN(dirent) strlen((dirent)->d_name)
396# else
397# define dirent direct
398# define NAMLEN(dirent) (dirent)->d_namlen
399# endif
400# ifdef HAVE_SYS_NDIR_H
401# include <sys/ndir.h>
402# endif
403# ifdef HAVE_SYS_DIR_H
404# include <sys/dir.h>
405# endif
406# ifdef HAVE_NDIR_H
407# include <ndir.h>
408# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000409#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000410
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000411#ifdef _MSC_VER
Victor Stinner5eca75d2020-04-15 15:07:31 +0200412# ifdef HAVE_DIRECT_H
413# include <direct.h>
414# endif
415# ifdef HAVE_IO_H
416# include <io.h>
417# endif
418# ifdef HAVE_PROCESS_H
419# include <process.h>
420# endif
421# ifndef IO_REPARSE_TAG_SYMLINK
422# define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
423# endif
424# ifndef IO_REPARSE_TAG_MOUNT_POINT
425# define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
426# endif
427# include "osdefs.h" // SEP
428# include <malloc.h>
429# include <windows.h>
430# include <shellapi.h> // ShellExecute()
431# include <lmcons.h> // UNLEN
432# define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000433#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000434
Tim Petersbc2e10e2002-03-03 23:17:02 +0000435#ifndef MAXPATHLEN
Victor Stinner5eca75d2020-04-15 15:07:31 +0200436# if defined(PATH_MAX) && PATH_MAX > 1024
437# define MAXPATHLEN PATH_MAX
438# else
439# define MAXPATHLEN 1024
440# endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000441#endif /* MAXPATHLEN */
442
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000443#ifdef UNION_WAIT
Victor Stinner5eca75d2020-04-15 15:07:31 +0200444 /* Emulate some macros on systems that have a union instead of macros */
445# ifndef WIFEXITED
446# define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
447# endif
448# ifndef WEXITSTATUS
449# define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
450# endif
451# ifndef WTERMSIG
452# define WTERMSIG(u_wait) ((u_wait).w_termsig)
453# endif
454# define WAIT_TYPE union wait
455# define WAIT_STATUS_INT(s) (s.w_status)
456#else
457 /* !UNION_WAIT */
458# define WAIT_TYPE int
459# define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000460#endif /* UNION_WAIT */
461
Greg Wardb48bc172000-03-01 21:51:56 +0000462/* Don't use the "_r" form if we don't need it (also, won't have a
463 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200464#if defined(HAVE_CTERMID_R)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200465# define USE_CTERMID_R
Greg Wardb48bc172000-03-01 21:51:56 +0000466#endif
467
Fred Drake699f3522000-06-29 21:12:41 +0000468/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000469#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000470#undef FSTAT
471#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200472#ifdef MS_WINDOWS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200473# define STAT win32_stat
474# define LSTAT win32_lstat
475# define FSTAT _Py_fstat_noraise
476# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000477#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200478# define STAT stat
479# define LSTAT lstat
480# define FSTAT fstat
481# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000482#endif
483
Tim Peters11b23062003-04-23 02:39:17 +0000484#if defined(MAJOR_IN_MKDEV)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200485# include <sys/mkdev.h>
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000486#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200487# if defined(MAJOR_IN_SYSMACROS)
488# include <sys/sysmacros.h>
489# endif
490# if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
491# include <sys/mkdev.h>
492# endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000493#endif
Fred Drake699f3522000-06-29 21:12:41 +0000494
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200495#ifdef MS_WINDOWS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200496# define INITFUNC PyInit_nt
497# define MODNAME "nt"
Victor Stinner6036e442015-03-08 01:58:04 +0100498#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200499# define INITFUNC PyInit_posix
500# define MODNAME "posix"
Victor Stinner6036e442015-03-08 01:58:04 +0100501#endif
502
jcea6c51d512018-01-28 14:00:08 +0100503#if defined(__sun)
504/* Something to implement in autoconf, not present in autoconf 2.69 */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200505# define HAVE_STRUCT_STAT_ST_FSTYPE 1
jcea6c51d512018-01-28 14:00:08 +0100506#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200507
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600508/* memfd_create is either defined in sys/mman.h or sys/memfd.h
509 * linux/memfd.h defines additional flags
510 */
511#ifdef HAVE_SYS_MMAN_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200512# include <sys/mman.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600513#endif
514#ifdef HAVE_SYS_MEMFD_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200515# include <sys/memfd.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600516#endif
517#ifdef HAVE_LINUX_MEMFD_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200518# include <linux/memfd.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600519#endif
520
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800521#ifdef _Py_MEMORY_SANITIZER
Victor Stinner5eca75d2020-04-15 15:07:31 +0200522# include <sanitizer/msan_interface.h>
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800523#endif
524
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200525#ifdef HAVE_FORK
526static void
527run_at_forkers(PyObject *lst, int reverse)
528{
529 Py_ssize_t i;
530 PyObject *cpy;
531
532 if (lst != NULL) {
533 assert(PyList_CheckExact(lst));
534
535 /* Use a list copy in case register_at_fork() is called from
536 * one of the callbacks.
537 */
538 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
539 if (cpy == NULL)
540 PyErr_WriteUnraisable(lst);
541 else {
542 if (reverse)
543 PyList_Reverse(cpy);
544 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
545 PyObject *func, *res;
546 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200547 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200548 if (res == NULL)
549 PyErr_WriteUnraisable(func);
550 else
551 Py_DECREF(res);
552 }
553 Py_DECREF(cpy);
554 }
555 }
556}
557
558void
559PyOS_BeforeFork(void)
560{
Victor Stinner81a7be32020-04-14 15:14:01 +0200561 run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200562
563 _PyImport_AcquireLock();
564}
565
566void
567PyOS_AfterFork_Parent(void)
568{
569 if (_PyImport_ReleaseLock() <= 0)
570 Py_FatalError("failed releasing import lock after fork");
571
Victor Stinner81a7be32020-04-14 15:14:01 +0200572 run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200573}
574
575void
576PyOS_AfterFork_Child(void)
577{
Victor Stinner26881c82020-06-02 15:51:37 +0200578 PyStatus status;
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200579 _PyRuntimeState *runtime = &_PyRuntime;
Victor Stinner26881c82020-06-02 15:51:37 +0200580
581 status = _PyGILState_Reinit(runtime);
582 if (_PyStatus_EXCEPTION(status)) {
583 goto fatal_error;
584 }
585
Victor Stinner317bab02020-06-02 18:44:54 +0200586 PyThreadState *tstate = _PyThreadState_GET();
587 _Py_EnsureTstateNotNULL(tstate);
588
589 status = _PyEval_ReInitThreads(tstate);
Victor Stinner26881c82020-06-02 15:51:37 +0200590 if (_PyStatus_EXCEPTION(status)) {
591 goto fatal_error;
592 }
593
594 status = _PyImport_ReInitLock();
595 if (_PyStatus_EXCEPTION(status)) {
596 goto fatal_error;
597 }
598
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200599 _PySignal_AfterFork();
Victor Stinner26881c82020-06-02 15:51:37 +0200600
601 status = _PyRuntimeState_ReInitThreads(runtime);
602 if (_PyStatus_EXCEPTION(status)) {
603 goto fatal_error;
604 }
605
606 status = _PyInterpreterState_DeleteExceptMain(runtime);
607 if (_PyStatus_EXCEPTION(status)) {
608 goto fatal_error;
609 }
Victor Stinner317bab02020-06-02 18:44:54 +0200610 assert(_PyThreadState_GET() == tstate);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200611
Victor Stinner317bab02020-06-02 18:44:54 +0200612 run_at_forkers(tstate->interp->after_forkers_child, 0);
Victor Stinner26881c82020-06-02 15:51:37 +0200613 return;
614
615fatal_error:
616 Py_ExitStatusException(status);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200617}
618
619static int
620register_at_forker(PyObject **lst, PyObject *func)
621{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700622 if (func == NULL) /* nothing to register? do nothing. */
623 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200624 if (*lst == NULL) {
625 *lst = PyList_New(0);
626 if (*lst == NULL)
627 return -1;
628 }
629 return PyList_Append(*lst, func);
630}
Victor Stinner87255be2020-04-07 23:11:49 +0200631#endif /* HAVE_FORK */
632
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200633
634/* Legacy wrapper */
635void
636PyOS_AfterFork(void)
637{
638#ifdef HAVE_FORK
639 PyOS_AfterFork_Child();
640#endif
641}
642
643
Victor Stinner6036e442015-03-08 01:58:04 +0100644#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200645/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700646void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
647void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200648 ULONG, struct _Py_stat_struct *);
649#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700650
Larry Hastings9cf065c2012-06-22 16:30:09 -0700651
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200652#ifndef MS_WINDOWS
653PyObject *
654_PyLong_FromUid(uid_t uid)
655{
656 if (uid == (uid_t)-1)
657 return PyLong_FromLong(-1);
658 return PyLong_FromUnsignedLong(uid);
659}
660
661PyObject *
662_PyLong_FromGid(gid_t gid)
663{
664 if (gid == (gid_t)-1)
665 return PyLong_FromLong(-1);
666 return PyLong_FromUnsignedLong(gid);
667}
668
669int
670_Py_Uid_Converter(PyObject *obj, void *p)
671{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700672 uid_t uid;
673 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200674 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200675 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700676 unsigned long uresult;
677
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300678 index = _PyNumber_Index(obj);
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700679 if (index == NULL) {
680 PyErr_Format(PyExc_TypeError,
681 "uid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800682 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200683 return 0;
684 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700685
686 /*
687 * Handling uid_t is complicated for two reasons:
688 * * Although uid_t is (always?) unsigned, it still
689 * accepts -1.
690 * * We don't know its size in advance--it may be
691 * bigger than an int, or it may be smaller than
692 * a long.
693 *
694 * So a bit of defensive programming is in order.
695 * Start with interpreting the value passed
696 * in as a signed long and see if it works.
697 */
698
699 result = PyLong_AsLongAndOverflow(index, &overflow);
700
701 if (!overflow) {
702 uid = (uid_t)result;
703
704 if (result == -1) {
705 if (PyErr_Occurred())
706 goto fail;
707 /* It's a legitimate -1, we're done. */
708 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200709 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700710
711 /* Any other negative number is disallowed. */
712 if (result < 0)
713 goto underflow;
714
715 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200716 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700717 (long)uid != result)
718 goto underflow;
719 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200720 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700721
722 if (overflow < 0)
723 goto underflow;
724
725 /*
726 * Okay, the value overflowed a signed long. If it
727 * fits in an *unsigned* long, it may still be okay,
728 * as uid_t may be unsigned long on this platform.
729 */
730 uresult = PyLong_AsUnsignedLong(index);
731 if (PyErr_Occurred()) {
732 if (PyErr_ExceptionMatches(PyExc_OverflowError))
733 goto overflow;
734 goto fail;
735 }
736
737 uid = (uid_t)uresult;
738
739 /*
740 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
741 * but this value would get interpreted as (uid_t)-1 by chown
742 * and its siblings. That's not what the user meant! So we
743 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100744 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700745 */
746 if (uid == (uid_t)-1)
747 goto overflow;
748
749 /* Ensure the value wasn't truncated. */
750 if (sizeof(uid_t) < sizeof(long) &&
751 (unsigned long)uid != uresult)
752 goto overflow;
753 /* fallthrough */
754
755success:
756 Py_DECREF(index);
757 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200758 return 1;
759
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700760underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200761 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700762 "uid is less than minimum");
763 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200764
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700765overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200766 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700767 "uid is greater than maximum");
768 /* fallthrough */
769
770fail:
771 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200772 return 0;
773}
774
775int
776_Py_Gid_Converter(PyObject *obj, void *p)
777{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700778 gid_t gid;
779 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200780 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200781 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700782 unsigned long uresult;
783
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300784 index = _PyNumber_Index(obj);
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700785 if (index == NULL) {
786 PyErr_Format(PyExc_TypeError,
787 "gid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800788 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200789 return 0;
790 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700791
792 /*
793 * Handling gid_t is complicated for two reasons:
794 * * Although gid_t is (always?) unsigned, it still
795 * accepts -1.
796 * * We don't know its size in advance--it may be
797 * bigger than an int, or it may be smaller than
798 * a long.
799 *
800 * So a bit of defensive programming is in order.
801 * Start with interpreting the value passed
802 * in as a signed long and see if it works.
803 */
804
805 result = PyLong_AsLongAndOverflow(index, &overflow);
806
807 if (!overflow) {
808 gid = (gid_t)result;
809
810 if (result == -1) {
811 if (PyErr_Occurred())
812 goto fail;
813 /* It's a legitimate -1, we're done. */
814 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200815 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700816
817 /* Any other negative number is disallowed. */
818 if (result < 0) {
819 goto underflow;
820 }
821
822 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200823 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700824 (long)gid != result)
825 goto underflow;
826 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200827 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700828
829 if (overflow < 0)
830 goto underflow;
831
832 /*
833 * Okay, the value overflowed a signed long. If it
834 * fits in an *unsigned* long, it may still be okay,
835 * as gid_t may be unsigned long on this platform.
836 */
837 uresult = PyLong_AsUnsignedLong(index);
838 if (PyErr_Occurred()) {
839 if (PyErr_ExceptionMatches(PyExc_OverflowError))
840 goto overflow;
841 goto fail;
842 }
843
844 gid = (gid_t)uresult;
845
846 /*
847 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
848 * but this value would get interpreted as (gid_t)-1 by chown
849 * and its siblings. That's not what the user meant! So we
850 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100851 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700852 */
853 if (gid == (gid_t)-1)
854 goto overflow;
855
856 /* Ensure the value wasn't truncated. */
857 if (sizeof(gid_t) < sizeof(long) &&
858 (unsigned long)gid != uresult)
859 goto overflow;
860 /* fallthrough */
861
862success:
863 Py_DECREF(index);
864 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200865 return 1;
866
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700867underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200868 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700869 "gid is less than minimum");
870 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200871
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700872overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200873 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700874 "gid is greater than maximum");
875 /* fallthrough */
876
877fail:
878 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200879 return 0;
880}
881#endif /* MS_WINDOWS */
882
883
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700884#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800885
886
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200887#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
888static int
889_Py_Dev_Converter(PyObject *obj, void *p)
890{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200891 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200892 if (PyErr_Occurred())
893 return 0;
894 return 1;
895}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800896#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200897
898
Larry Hastings9cf065c2012-06-22 16:30:09 -0700899#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400900/*
901 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
902 * without the int cast, the value gets interpreted as uint (4291925331),
903 * which doesn't play nicely with all the initializer lines in this file that
904 * look like this:
905 * int dir_fd = DEFAULT_DIR_FD;
906 */
907#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700908#else
909#define DEFAULT_DIR_FD (-100)
910#endif
911
912static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300913_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200914{
915 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700916 long long_value;
917
Serhiy Storchaka5f4b229d2020-05-28 10:33:45 +0300918 PyObject *index = _PyNumber_Index(o);
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700919 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700920 return 0;
921 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700922
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300923 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700924 long_value = PyLong_AsLongAndOverflow(index, &overflow);
925 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300926 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200927 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700928 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700929 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700930 return 0;
931 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200932 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700933 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700934 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700935 return 0;
936 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700937
Larry Hastings9cf065c2012-06-22 16:30:09 -0700938 *p = (int)long_value;
939 return 1;
940}
941
942static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200943dir_fd_converter(PyObject *o, void *p)
944{
945 if (o == Py_None) {
946 *(int *)p = DEFAULT_DIR_FD;
947 return 1;
948 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300949 else if (PyIndex_Check(o)) {
950 return _fd_converter(o, (int *)p);
951 }
952 else {
953 PyErr_Format(PyExc_TypeError,
954 "argument should be integer or None, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800955 _PyType_Name(Py_TYPE(o)));
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300956 return 0;
957 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700958}
959
Eddie Elizondob3966632019-11-05 07:16:14 -0800960typedef struct {
961 PyObject *billion;
Eddie Elizondob3966632019-11-05 07:16:14 -0800962 PyObject *DirEntryType;
963 PyObject *ScandirIteratorType;
964#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
965 PyObject *SchedParamType;
966#endif
967 PyObject *StatResultType;
968 PyObject *StatVFSResultType;
969 PyObject *TerminalSizeType;
970 PyObject *TimesResultType;
971 PyObject *UnameResultType;
972#if defined(HAVE_WAITID) && !defined(__APPLE__)
973 PyObject *WaitidResultType;
974#endif
975#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
976 PyObject *struct_rusage;
977#endif
978 PyObject *st_mode;
979} _posixstate;
980
Eddie Elizondob3966632019-11-05 07:16:14 -0800981
Hai Shif707d942020-03-16 21:15:01 +0800982static inline _posixstate*
983get_posix_state(PyObject *module)
984{
985 void *state = PyModule_GetState(module);
986 assert(state != NULL);
987 return (_posixstate *)state;
988}
989
Larry Hastings9cf065c2012-06-22 16:30:09 -0700990/*
991 * A PyArg_ParseTuple "converter" function
992 * that handles filesystem paths in the manner
993 * preferred by the os module.
994 *
995 * path_converter accepts (Unicode) strings and their
996 * subclasses, and bytes and their subclasses. What
997 * it does with the argument depends on the platform:
998 *
999 * * On Windows, if we get a (Unicode) string we
1000 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -07001001 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001002 *
1003 * * On all other platforms, strings are encoded
1004 * to bytes using PyUnicode_FSConverter, then we
1005 * extract the char * from the bytes object and
1006 * return that.
1007 *
1008 * path_converter also optionally accepts signed
1009 * integers (representing open file descriptors) instead
1010 * of path strings.
1011 *
1012 * Input fields:
1013 * path.nullable
1014 * If nonzero, the path is permitted to be None.
1015 * path.allow_fd
1016 * If nonzero, the path is permitted to be a file handle
1017 * (a signed int) instead of a string.
1018 * path.function_name
1019 * If non-NULL, path_converter will use that as the name
1020 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -07001021 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001022 * path.argument_name
1023 * If non-NULL, path_converter will use that as the name
1024 * of the parameter in error messages.
1025 * (If path.argument_name is NULL it uses "path".)
1026 *
1027 * Output fields:
1028 * path.wide
1029 * Points to the path if it was expressed as Unicode
1030 * and was not encoded. (Only used on Windows.)
1031 * path.narrow
1032 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -07001033 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +00001034 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -07001035 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001036 * path.fd
1037 * Contains a file descriptor if path.accept_fd was true
1038 * and the caller provided a signed integer instead of any
1039 * sort of string.
1040 *
1041 * WARNING: if your "path" parameter is optional, and is
1042 * unspecified, path_converter will never get called.
1043 * So if you set allow_fd, you *MUST* initialize path.fd = -1
1044 * yourself!
1045 * path.length
1046 * The length of the path in characters, if specified as
1047 * a string.
1048 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +08001049 * The original object passed in (if get a PathLike object,
1050 * the result of PyOS_FSPath() is treated as the original object).
1051 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -07001052 * path.cleanup
1053 * For internal use only. May point to a temporary object.
1054 * (Pay no attention to the man behind the curtain.)
1055 *
1056 * At most one of path.wide or path.narrow will be non-NULL.
1057 * If path was None and path.nullable was set,
1058 * or if path was an integer and path.allow_fd was set,
1059 * both path.wide and path.narrow will be NULL
1060 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +02001061 *
Larry Hastings9cf065c2012-06-22 16:30:09 -07001062 * path_converter takes care to not write to the path_t
1063 * unless it's successful. However it must reset the
1064 * "cleanup" field each time it's called.
1065 *
1066 * Use as follows:
1067 * path_t path;
1068 * memset(&path, 0, sizeof(path));
1069 * PyArg_ParseTuple(args, "O&", path_converter, &path);
1070 * // ... use values from path ...
1071 * path_cleanup(&path);
1072 *
1073 * (Note that if PyArg_Parse fails you don't need to call
1074 * path_cleanup(). However it is safe to do so.)
1075 */
1076typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +01001077 const char *function_name;
1078 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001079 int nullable;
1080 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001081 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -07001082#ifdef MS_WINDOWS
1083 BOOL narrow;
1084#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001085 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -07001086#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001087 int fd;
1088 Py_ssize_t length;
1089 PyObject *object;
1090 PyObject *cleanup;
1091} path_t;
1092
Steve Dowercc16be82016-09-08 10:35:16 -07001093#ifdef MS_WINDOWS
1094#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
1095 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
1096#else
Larry Hastings2f936352014-08-05 14:04:04 +10001097#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
1098 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -07001099#endif
Larry Hastings31826802013-10-19 00:09:25 -07001100
Larry Hastings9cf065c2012-06-22 16:30:09 -07001101static void
Xiang Zhang04316c42017-01-08 23:26:57 +08001102path_cleanup(path_t *path)
1103{
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001104#if !USE_UNICODE_WCHAR_CACHE
1105 wchar_t *wide = (wchar_t *)path->wide;
1106 path->wide = NULL;
1107 PyMem_Free(wide);
1108#endif /* USE_UNICODE_WCHAR_CACHE */
Xiang Zhang04316c42017-01-08 23:26:57 +08001109 Py_CLEAR(path->object);
1110 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001111}
1112
1113static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001114path_converter(PyObject *o, void *p)
1115{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001116 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +08001117 PyObject *bytes = NULL;
1118 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001119 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001120 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -07001121#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +08001122 PyObject *wo = NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001123 wchar_t *wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001124#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001125
1126#define FORMAT_EXCEPTION(exc, fmt) \
1127 PyErr_Format(exc, "%s%s" fmt, \
1128 path->function_name ? path->function_name : "", \
1129 path->function_name ? ": " : "", \
1130 path->argument_name ? path->argument_name : "path")
1131
1132 /* Py_CLEANUP_SUPPORTED support */
1133 if (o == NULL) {
1134 path_cleanup(path);
1135 return 1;
1136 }
1137
Brett Cannon3f9183b2016-08-26 14:44:48 -07001138 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +08001139 path->object = path->cleanup = NULL;
1140 /* path->object owns a reference to the original object */
1141 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001142
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001143 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001144 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001145#ifdef MS_WINDOWS
1146 path->narrow = FALSE;
1147#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001148 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001149#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001150 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001151 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001152 }
1153
Brett Cannon3f9183b2016-08-26 14:44:48 -07001154 /* Only call this here so that we don't treat the return value of
1155 os.fspath() as an fd or buffer. */
1156 is_index = path->allow_fd && PyIndex_Check(o);
1157 is_buffer = PyObject_CheckBuffer(o);
1158 is_bytes = PyBytes_Check(o);
1159 is_unicode = PyUnicode_Check(o);
1160
1161 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
1162 /* Inline PyOS_FSPath() for better error messages. */
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001163 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001164
1165 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1166 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001167 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001168 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001169 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001170 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001171 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001172 goto error_exit;
1173 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001174 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001175 is_unicode = 1;
1176 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001177 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001178 is_bytes = 1;
1179 }
1180 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001181 PyErr_Format(PyExc_TypeError,
1182 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -08001183 "not %.200s", _PyType_Name(Py_TYPE(o)),
1184 _PyType_Name(Py_TYPE(res)));
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001185 Py_DECREF(res);
1186 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001187 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001188
1189 /* still owns a reference to the original object */
1190 Py_DECREF(o);
1191 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001192 }
1193
1194 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001195#ifdef MS_WINDOWS
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001196#if USE_UNICODE_WCHAR_CACHE
1197_Py_COMP_DIAG_PUSH
1198_Py_COMP_DIAG_IGNORE_DEPR_DECLS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001199 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001200_Py_COMP_DIAG_POP
1201#else /* USE_UNICODE_WCHAR_CACHE */
1202 wide = PyUnicode_AsWideCharString(o, &length);
1203#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner59799a82013-11-13 14:17:30 +01001204 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001205 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001206 }
Victor Stinner59799a82013-11-13 14:17:30 +01001207 if (length > 32767) {
1208 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001209 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001210 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001211 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001212 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001213 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001214 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001215
1216 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001217 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001218 path->fd = -1;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001219#if !USE_UNICODE_WCHAR_CACHE
1220 wide = NULL;
1221#endif /* USE_UNICODE_WCHAR_CACHE */
Xiang Zhang04316c42017-01-08 23:26:57 +08001222 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001223#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001224 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001225 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001226 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001227#endif
1228 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001229 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001230 bytes = o;
1231 Py_INCREF(bytes);
1232 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001233 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001234 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001235 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001236 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1237 "%s%s%s should be %s, not %.200s",
1238 path->function_name ? path->function_name : "",
1239 path->function_name ? ": " : "",
1240 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001241 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1242 "integer or None" :
1243 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1244 path->nullable ? "string, bytes, os.PathLike or None" :
1245 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001246 _PyType_Name(Py_TYPE(o)))) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001247 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001248 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001249 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001250 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001251 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001252 }
1253 }
Steve Dowercc16be82016-09-08 10:35:16 -07001254 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001255 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001256 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001257 }
1258 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001259#ifdef MS_WINDOWS
1260 path->narrow = FALSE;
1261#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001262 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001263#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001264 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001265 }
1266 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001267 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001268 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1269 path->function_name ? path->function_name : "",
1270 path->function_name ? ": " : "",
1271 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001272 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1273 "integer or None" :
1274 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1275 path->nullable ? "string, bytes, os.PathLike or None" :
1276 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001277 _PyType_Name(Py_TYPE(o)));
Xiang Zhang04316c42017-01-08 23:26:57 +08001278 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001279 }
1280
Larry Hastings9cf065c2012-06-22 16:30:09 -07001281 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001282 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001283 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001284 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001285 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001286 }
1287
Steve Dowercc16be82016-09-08 10:35:16 -07001288#ifdef MS_WINDOWS
1289 wo = PyUnicode_DecodeFSDefaultAndSize(
1290 narrow,
1291 length
1292 );
1293 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001294 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001295 }
1296
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001297#if USE_UNICODE_WCHAR_CACHE
1298_Py_COMP_DIAG_PUSH
1299_Py_COMP_DIAG_IGNORE_DEPR_DECLS
Xiang Zhang04316c42017-01-08 23:26:57 +08001300 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001301_Py_COMP_DIAG_POP
1302#else /* USE_UNICODE_WCHAR_CACHE */
1303 wide = PyUnicode_AsWideCharString(wo, &length);
1304 Py_DECREF(wo);
1305#endif /* USE_UNICODE_WCHAR_CACHE */
Steve Dowercc16be82016-09-08 10:35:16 -07001306 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001307 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001308 }
1309 if (length > 32767) {
1310 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001311 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001312 }
1313 if (wcslen(wide) != length) {
1314 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001315 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001316 }
1317 path->wide = wide;
1318 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001319 Py_DECREF(bytes);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001320#if USE_UNICODE_WCHAR_CACHE
1321 path->cleanup = wo;
1322#else /* USE_UNICODE_WCHAR_CACHE */
1323 wide = NULL;
1324#endif /* USE_UNICODE_WCHAR_CACHE */
Steve Dowercc16be82016-09-08 10:35:16 -07001325#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001326 path->wide = NULL;
1327 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001328 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001329 /* Still a reference owned by path->object, don't have to
1330 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001331 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001332 }
1333 else {
1334 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001335 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001336#endif
1337 path->fd = -1;
1338
1339 success_exit:
1340 path->length = length;
1341 path->object = o;
1342 return Py_CLEANUP_SUPPORTED;
1343
1344 error_exit:
1345 Py_XDECREF(o);
1346 Py_XDECREF(bytes);
1347#ifdef MS_WINDOWS
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001348#if USE_UNICODE_WCHAR_CACHE
Xiang Zhang04316c42017-01-08 23:26:57 +08001349 Py_XDECREF(wo);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +03001350#else /* USE_UNICODE_WCHAR_CACHE */
1351 PyMem_Free(wide);
1352#endif /* USE_UNICODE_WCHAR_CACHE */
Xiang Zhang04316c42017-01-08 23:26:57 +08001353#endif
1354 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001355}
1356
1357static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001358argument_unavailable_error(const char *function_name, const char *argument_name)
1359{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001360 PyErr_Format(PyExc_NotImplementedError,
1361 "%s%s%s unavailable on this platform",
1362 (function_name != NULL) ? function_name : "",
1363 (function_name != NULL) ? ": ": "",
1364 argument_name);
1365}
1366
1367static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001368dir_fd_unavailable(PyObject *o, void *p)
1369{
1370 int dir_fd;
1371 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001372 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001373 if (dir_fd != DEFAULT_DIR_FD) {
1374 argument_unavailable_error(NULL, "dir_fd");
1375 return 0;
1376 }
1377 *(int *)p = dir_fd;
1378 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001379}
1380
1381static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001382fd_specified(const char *function_name, int fd)
1383{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001384 if (fd == -1)
1385 return 0;
1386
1387 argument_unavailable_error(function_name, "fd");
1388 return 1;
1389}
1390
1391static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001392follow_symlinks_specified(const char *function_name, int follow_symlinks)
1393{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001394 if (follow_symlinks)
1395 return 0;
1396
1397 argument_unavailable_error(function_name, "follow_symlinks");
1398 return 1;
1399}
1400
1401static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001402path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1403{
Steve Dowercc16be82016-09-08 10:35:16 -07001404 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1405#ifndef MS_WINDOWS
1406 && !path->narrow
1407#endif
1408 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001409 PyErr_Format(PyExc_ValueError,
1410 "%s: can't specify dir_fd without matching path",
1411 function_name);
1412 return 1;
1413 }
1414 return 0;
1415}
1416
1417static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001418dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1419{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001420 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1421 PyErr_Format(PyExc_ValueError,
1422 "%s: can't specify both dir_fd and fd",
1423 function_name);
1424 return 1;
1425 }
1426 return 0;
1427}
1428
1429static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001430fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1431 int follow_symlinks)
1432{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001433 if ((fd > 0) && (!follow_symlinks)) {
1434 PyErr_Format(PyExc_ValueError,
1435 "%s: cannot use fd and follow_symlinks together",
1436 function_name);
1437 return 1;
1438 }
1439 return 0;
1440}
1441
1442static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001443dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1444 int follow_symlinks)
1445{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001446 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1447 PyErr_Format(PyExc_ValueError,
1448 "%s: cannot use dir_fd and follow_symlinks together",
1449 function_name);
1450 return 1;
1451 }
1452 return 0;
1453}
1454
Larry Hastings2f936352014-08-05 14:04:04 +10001455#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001456 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001457#else
Larry Hastings2f936352014-08-05 14:04:04 +10001458 typedef off_t Py_off_t;
1459#endif
1460
1461static int
1462Py_off_t_converter(PyObject *arg, void *addr)
1463{
1464#ifdef HAVE_LARGEFILE_SUPPORT
1465 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1466#else
1467 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001468#endif
1469 if (PyErr_Occurred())
1470 return 0;
1471 return 1;
1472}
Larry Hastings2f936352014-08-05 14:04:04 +10001473
1474static PyObject *
1475PyLong_FromPy_off_t(Py_off_t offset)
1476{
1477#ifdef HAVE_LARGEFILE_SUPPORT
1478 return PyLong_FromLongLong(offset);
1479#else
1480 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001481#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001482}
1483
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001484#ifdef HAVE_SIGSET_T
1485/* Convert an iterable of integers to a sigset.
1486 Return 1 on success, return 0 and raise an exception on error. */
1487int
1488_Py_Sigset_Converter(PyObject *obj, void *addr)
1489{
1490 sigset_t *mask = (sigset_t *)addr;
1491 PyObject *iterator, *item;
1492 long signum;
1493 int overflow;
1494
Rémi Lapeyref0900192019-05-04 01:30:53 +02001495 // The extra parens suppress the unreachable-code warning with clang on MacOS
1496 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001497 /* Probably only if mask == NULL. */
1498 PyErr_SetFromErrno(PyExc_OSError);
1499 return 0;
1500 }
1501
1502 iterator = PyObject_GetIter(obj);
1503 if (iterator == NULL) {
1504 return 0;
1505 }
1506
1507 while ((item = PyIter_Next(iterator)) != NULL) {
1508 signum = PyLong_AsLongAndOverflow(item, &overflow);
1509 Py_DECREF(item);
1510 if (signum <= 0 || signum >= NSIG) {
1511 if (overflow || signum != -1 || !PyErr_Occurred()) {
1512 PyErr_Format(PyExc_ValueError,
1513 "signal number %ld out of range", signum);
1514 }
1515 goto error;
1516 }
1517 if (sigaddset(mask, (int)signum)) {
1518 if (errno != EINVAL) {
1519 /* Probably impossible */
1520 PyErr_SetFromErrno(PyExc_OSError);
1521 goto error;
1522 }
1523 /* For backwards compatibility, allow idioms such as
1524 * `range(1, NSIG)` but warn about invalid signal numbers
1525 */
1526 const char msg[] =
1527 "invalid signal number %ld, please use valid_signals()";
1528 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1529 goto error;
1530 }
1531 }
1532 }
1533 if (!PyErr_Occurred()) {
1534 Py_DECREF(iterator);
1535 return 1;
1536 }
1537
1538error:
1539 Py_DECREF(iterator);
1540 return 0;
1541}
1542#endif /* HAVE_SIGSET_T */
1543
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001544#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001545
1546static int
Brian Curtind25aef52011-06-13 15:16:04 -05001547win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001548{
Martin Panter70214ad2016-08-04 02:38:59 +00001549 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1550 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001551 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001552
1553 if (0 == DeviceIoControl(
1554 reparse_point_handle,
1555 FSCTL_GET_REPARSE_POINT,
1556 NULL, 0, /* in buffer */
1557 target_buffer, sizeof(target_buffer),
1558 &n_bytes_returned,
1559 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001560 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001561
1562 if (reparse_tag)
1563 *reparse_tag = rdb->ReparseTag;
1564
Brian Curtind25aef52011-06-13 15:16:04 -05001565 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001566}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001567
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001568#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001569
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001570/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001571#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001572/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001573** environ directly, we must obtain it with _NSGetEnviron(). See also
1574** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001575*/
1576#include <crt_externs.h>
pxinwrf2d7ac72019-05-21 18:46:37 +08001577#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001578extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001579#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001580
Barry Warsaw53699e91996-12-10 23:23:01 +00001581static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001582convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001583{
Victor Stinner8c62be82010-05-06 00:08:46 +00001584 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001585#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001586 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001587#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001588 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001589#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001590
Victor Stinner8c62be82010-05-06 00:08:46 +00001591 d = PyDict_New();
1592 if (d == NULL)
1593 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001594#ifdef MS_WINDOWS
1595 /* _wenviron must be initialized in this way if the program is started
1596 through main() instead of wmain(). */
1597 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001598 e = _wenviron;
Benoit Hudson723f71a2019-12-06 14:15:03 -05001599#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1600 /* environ is not accessible as an extern in a shared object on OSX; use
1601 _NSGetEnviron to resolve it. The value changes if you add environment
1602 variables between calls to Py_Initialize, so don't cache the value. */
1603 e = *_NSGetEnviron();
Victor Stinner8c62be82010-05-06 00:08:46 +00001604#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001605 e = environ;
1606#endif
1607 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001608 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001609 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001610 PyObject *k;
1611 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001612#ifdef MS_WINDOWS
1613 const wchar_t *p = wcschr(*e, L'=');
1614#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001615 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001616#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001617 if (p == NULL)
1618 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001619#ifdef MS_WINDOWS
1620 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1621#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001622 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001623#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001624 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001625 Py_DECREF(d);
1626 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001627 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001628#ifdef MS_WINDOWS
1629 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1630#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001631 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001632#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001633 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001634 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001635 Py_DECREF(d);
1636 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001637 }
Serhiy Storchakab510e102020-10-26 12:47:57 +02001638 if (PyDict_SetDefault(d, k, v) == NULL) {
1639 Py_DECREF(v);
1640 Py_DECREF(k);
1641 Py_DECREF(d);
1642 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001643 }
1644 Py_DECREF(k);
1645 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001646 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001647 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001648}
1649
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001650/* Set a POSIX-specific error from errno, and return NULL */
1651
Barry Warsawd58d7641998-07-23 16:14:40 +00001652static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001653posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001654{
Victor Stinner8c62be82010-05-06 00:08:46 +00001655 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001656}
Mark Hammondef8b6542001-05-13 08:04:26 +00001657
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001658#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001659static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001660win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001661{
Victor Stinner8c62be82010-05-06 00:08:46 +00001662 /* XXX We should pass the function name along in the future.
1663 (winreg.c also wants to pass the function name.)
1664 This would however require an additional param to the
1665 Windows error object, which is non-trivial.
1666 */
1667 errno = GetLastError();
1668 if (filename)
1669 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1670 else
1671 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001672}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001673
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001674static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001675win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001676{
1677 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001678 if (filename)
1679 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001680 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001681 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001682 filename);
1683 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001684 return PyErr_SetFromWindowsErr(err);
1685}
1686
1687static PyObject *
1688win32_error_object(const char* function, PyObject* filename)
1689{
1690 errno = GetLastError();
1691 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001692}
1693
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001694#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001695
Larry Hastings9cf065c2012-06-22 16:30:09 -07001696static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001697posix_path_object_error(PyObject *path)
1698{
1699 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1700}
1701
1702static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001703path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001704{
1705#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001706 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1707 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001708#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001709 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001710#endif
1711}
1712
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001713static PyObject *
1714path_object_error2(PyObject *path, PyObject *path2)
1715{
1716#ifdef MS_WINDOWS
1717 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1718 PyExc_OSError, 0, path, path2);
1719#else
1720 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1721#endif
1722}
1723
1724static PyObject *
1725path_error(path_t *path)
1726{
1727 return path_object_error(path->object);
1728}
Larry Hastings31826802013-10-19 00:09:25 -07001729
Larry Hastingsb0827312014-02-09 22:05:19 -08001730static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001731posix_path_error(path_t *path)
1732{
1733 return posix_path_object_error(path->object);
1734}
1735
1736static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001737path_error2(path_t *path, path_t *path2)
1738{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001739 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001740}
1741
1742
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001743/* POSIX generic methods */
1744
Larry Hastings2f936352014-08-05 14:04:04 +10001745static PyObject *
1746posix_fildes_fd(int fd, int (*func)(int))
1747{
1748 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001749 int async_err = 0;
1750
1751 do {
1752 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001753 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001754 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001755 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001756 Py_END_ALLOW_THREADS
1757 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1758 if (res != 0)
1759 return (!async_err) ? posix_error() : NULL;
1760 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001761}
Guido van Rossum21142a01999-01-08 21:05:37 +00001762
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001763
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001764#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001765/* This is a reimplementation of the C library's chdir function,
1766 but one that produces Win32 errors instead of DOS error codes.
1767 chdir is essentially a wrapper around SetCurrentDirectory; however,
1768 it also needs to set "magic" environment variables indicating
1769 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001770static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001771win32_wchdir(LPCWSTR path)
1772{
Victor Stinnered537822015-12-13 21:40:26 +01001773 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001774 int result;
1775 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001776
Victor Stinner8c62be82010-05-06 00:08:46 +00001777 if(!SetCurrentDirectoryW(path))
1778 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001779 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001780 if (!result)
1781 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001782 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001783 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001784 if (!new_path) {
1785 SetLastError(ERROR_OUTOFMEMORY);
1786 return FALSE;
1787 }
1788 result = GetCurrentDirectoryW(result, new_path);
1789 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001790 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001791 return FALSE;
1792 }
1793 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001794 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1795 wcsncmp(new_path, L"//", 2) == 0);
1796 if (!is_unc_like_path) {
1797 env[1] = new_path[0];
1798 result = SetEnvironmentVariableW(env, new_path);
1799 }
Victor Stinnered537822015-12-13 21:40:26 +01001800 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001801 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001802 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001803}
1804#endif
1805
Martin v. Löwis14694662006-02-03 12:54:16 +00001806#ifdef MS_WINDOWS
1807/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1808 - time stamps are restricted to second resolution
1809 - file modification times suffer from forth-and-back conversions between
1810 UTC and local time
1811 Therefore, we implement our own stat, based on the Win32 API directly.
1812*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001813#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001814#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001815#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001816
Victor Stinner6036e442015-03-08 01:58:04 +01001817static void
Steve Dowercc16be82016-09-08 10:35:16 -07001818find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1819 BY_HANDLE_FILE_INFORMATION *info,
1820 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001821{
1822 memset(info, 0, sizeof(*info));
1823 info->dwFileAttributes = pFileData->dwFileAttributes;
1824 info->ftCreationTime = pFileData->ftCreationTime;
1825 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1826 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1827 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1828 info->nFileSizeLow = pFileData->nFileSizeLow;
1829/* info->nNumberOfLinks = 1; */
1830 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1831 *reparse_tag = pFileData->dwReserved0;
1832 else
1833 *reparse_tag = 0;
1834}
1835
Guido van Rossumd8faa362007-04-27 19:54:29 +00001836static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001837attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001838{
Victor Stinner8c62be82010-05-06 00:08:46 +00001839 HANDLE hFindFile;
1840 WIN32_FIND_DATAW FileData;
1841 hFindFile = FindFirstFileW(pszFile, &FileData);
1842 if (hFindFile == INVALID_HANDLE_VALUE)
1843 return FALSE;
1844 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001845 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001846 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001847}
1848
Brian Curtind25aef52011-06-13 15:16:04 -05001849static int
Steve Dowercc16be82016-09-08 10:35:16 -07001850win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001851 BOOL traverse)
1852{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001853 HANDLE hFile;
1854 BY_HANDLE_FILE_INFORMATION fileInfo;
1855 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1856 DWORD fileType, error;
1857 BOOL isUnhandledTag = FALSE;
1858 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001859
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001860 DWORD access = FILE_READ_ATTRIBUTES;
1861 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1862 if (!traverse) {
1863 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1864 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001865
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001866 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001867 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001868 /* Either the path doesn't exist, or the caller lacks access. */
1869 error = GetLastError();
1870 switch (error) {
1871 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1872 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1873 /* Try reading the parent directory. */
1874 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1875 /* Cannot read the parent directory. */
1876 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001877 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001878 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001879 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1880 if (traverse ||
1881 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1882 /* The stat call has to traverse but cannot, so fail. */
1883 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001884 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001885 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001886 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001887 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001888
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001889 case ERROR_INVALID_PARAMETER:
1890 /* \\.\con requires read or write access. */
1891 hFile = CreateFileW(path, access | GENERIC_READ,
1892 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1893 OPEN_EXISTING, flags, NULL);
1894 if (hFile == INVALID_HANDLE_VALUE) {
1895 SetLastError(error);
1896 return -1;
1897 }
1898 break;
1899
1900 case ERROR_CANT_ACCESS_FILE:
1901 /* bpo37834: open unhandled reparse points if traverse fails. */
1902 if (traverse) {
1903 traverse = FALSE;
1904 isUnhandledTag = TRUE;
1905 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1906 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1907 }
1908 if (hFile == INVALID_HANDLE_VALUE) {
1909 SetLastError(error);
1910 return -1;
1911 }
1912 break;
1913
1914 default:
1915 return -1;
1916 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001917 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001918
1919 if (hFile != INVALID_HANDLE_VALUE) {
1920 /* Handle types other than files on disk. */
1921 fileType = GetFileType(hFile);
1922 if (fileType != FILE_TYPE_DISK) {
1923 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1924 retval = -1;
1925 goto cleanup;
1926 }
1927 DWORD fileAttributes = GetFileAttributesW(path);
1928 memset(result, 0, sizeof(*result));
1929 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1930 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1931 /* \\.\pipe\ or \\.\mailslot\ */
1932 result->st_mode = _S_IFDIR;
1933 } else if (fileType == FILE_TYPE_CHAR) {
1934 /* \\.\nul */
1935 result->st_mode = _S_IFCHR;
1936 } else if (fileType == FILE_TYPE_PIPE) {
1937 /* \\.\pipe\spam */
1938 result->st_mode = _S_IFIFO;
1939 }
1940 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1941 goto cleanup;
1942 }
1943
1944 /* Query the reparse tag, and traverse a non-link. */
1945 if (!traverse) {
1946 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1947 &tagInfo, sizeof(tagInfo))) {
1948 /* Allow devices that do not support FileAttributeTagInfo. */
1949 switch (GetLastError()) {
1950 case ERROR_INVALID_PARAMETER:
1951 case ERROR_INVALID_FUNCTION:
1952 case ERROR_NOT_SUPPORTED:
1953 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1954 tagInfo.ReparseTag = 0;
1955 break;
1956 default:
1957 retval = -1;
1958 goto cleanup;
1959 }
1960 } else if (tagInfo.FileAttributes &
1961 FILE_ATTRIBUTE_REPARSE_POINT) {
1962 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1963 if (isUnhandledTag) {
1964 /* Traversing previously failed for either this link
1965 or its target. */
1966 SetLastError(ERROR_CANT_ACCESS_FILE);
1967 retval = -1;
1968 goto cleanup;
1969 }
1970 /* Traverse a non-link, but not if traversing already failed
1971 for an unhandled tag. */
1972 } else if (!isUnhandledTag) {
1973 CloseHandle(hFile);
1974 return win32_xstat_impl(path, result, TRUE);
1975 }
1976 }
1977 }
1978
1979 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1980 switch (GetLastError()) {
1981 case ERROR_INVALID_PARAMETER:
1982 case ERROR_INVALID_FUNCTION:
1983 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001984 /* Volumes and physical disks are block devices, e.g.
1985 \\.\C: and \\.\PhysicalDrive0. */
1986 memset(result, 0, sizeof(*result));
1987 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001988 goto cleanup;
1989 }
Steve Dower772ec0f2019-09-04 14:42:54 -07001990 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001991 goto cleanup;
1992 }
1993 }
1994
1995 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1996
1997 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1998 /* Fix the file execute permissions. This hack sets S_IEXEC if
1999 the filename has an extension that is commonly used by files
2000 that CreateProcessW can execute. A real implementation calls
2001 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
2002 AccessCheck to check for generic read, write, and execute
2003 access. */
2004 const wchar_t *fileExtension = wcsrchr(path, '.');
2005 if (fileExtension) {
2006 if (_wcsicmp(fileExtension, L".exe") == 0 ||
2007 _wcsicmp(fileExtension, L".bat") == 0 ||
2008 _wcsicmp(fileExtension, L".cmd") == 0 ||
2009 _wcsicmp(fileExtension, L".com") == 0) {
2010 result->st_mode |= 0111;
2011 }
2012 }
2013 }
2014
2015cleanup:
2016 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07002017 /* Preserve last error if we are failing */
2018 error = retval ? GetLastError() : 0;
2019 if (!CloseHandle(hFile)) {
2020 retval = -1;
2021 } else if (retval) {
2022 /* Restore last error */
2023 SetLastError(error);
2024 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002025 }
2026
2027 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00002028}
2029
2030static int
Steve Dowercc16be82016-09-08 10:35:16 -07002031win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00002032{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002033 /* Protocol violation: we explicitly clear errno, instead of
2034 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05002035 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002036 errno = 0;
2037 return code;
2038}
Brian Curtind25aef52011-06-13 15:16:04 -05002039/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00002040
2041 In Posix, stat automatically traverses symlinks and returns the stat
2042 structure for the target. In Windows, the equivalent GetFileAttributes by
2043 default does not traverse symlinks and instead returns attributes for
2044 the symlink.
2045
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002046 Instead, we will open the file (which *does* traverse symlinks by default)
2047 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00002048
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00002049static int
Steve Dowercc16be82016-09-08 10:35:16 -07002050win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00002051{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002052 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00002053}
2054
Victor Stinner8c62be82010-05-06 00:08:46 +00002055static int
Steve Dowercc16be82016-09-08 10:35:16 -07002056win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00002057{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00002058 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00002059}
2060
Martin v. Löwis14694662006-02-03 12:54:16 +00002061#endif /* MS_WINDOWS */
2062
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002063PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002064"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002065This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002066 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002067or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
2068\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002069Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
2070or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002071\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002072See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002073
2074static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002075 {"st_mode", "protection bits"},
2076 {"st_ino", "inode"},
2077 {"st_dev", "device"},
2078 {"st_nlink", "number of hard links"},
2079 {"st_uid", "user ID of owner"},
2080 {"st_gid", "group ID of owner"},
2081 {"st_size", "total size, in bytes"},
2082 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
2083 {NULL, "integer time of last access"},
2084 {NULL, "integer time of last modification"},
2085 {NULL, "integer time of last change"},
2086 {"st_atime", "time of last access"},
2087 {"st_mtime", "time of last modification"},
2088 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07002089 {"st_atime_ns", "time of last access in nanoseconds"},
2090 {"st_mtime_ns", "time of last modification in nanoseconds"},
2091 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002092#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002093 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002094#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002095#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002096 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002097#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002098#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002099 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002100#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002101#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002102 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002103#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002104#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002105 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002106#endif
2107#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002108 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002109#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002110#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2111 {"st_file_attributes", "Windows file attribute bits"},
2112#endif
jcea6c51d512018-01-28 14:00:08 +01002113#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2114 {"st_fstype", "Type of filesystem"},
2115#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002116#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2117 {"st_reparse_tag", "Windows reparse tag"},
2118#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002119 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002120};
2121
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002122#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07002123#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002124#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07002125#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002126#endif
2127
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002128#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002129#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
2130#else
2131#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
2132#endif
2133
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002134#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002135#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
2136#else
2137#define ST_RDEV_IDX ST_BLOCKS_IDX
2138#endif
2139
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002140#ifdef HAVE_STRUCT_STAT_ST_FLAGS
2141#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
2142#else
2143#define ST_FLAGS_IDX ST_RDEV_IDX
2144#endif
2145
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002146#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00002147#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002148#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00002149#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002150#endif
2151
2152#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
2153#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
2154#else
2155#define ST_BIRTHTIME_IDX ST_GEN_IDX
2156#endif
2157
Zachary Ware63f277b2014-06-19 09:46:37 -05002158#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2159#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
2160#else
2161#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
2162#endif
2163
jcea6c51d512018-01-28 14:00:08 +01002164#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2165#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2166#else
2167#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2168#endif
2169
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002170#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2171#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2172#else
2173#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2174#endif
2175
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002176static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002177 "stat_result", /* name */
2178 stat_result__doc__, /* doc */
2179 stat_result_fields,
2180 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002181};
2182
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002183PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002184"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2185This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002186 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002187or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002188\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002189See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002190
2191static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002192 {"f_bsize", },
2193 {"f_frsize", },
2194 {"f_blocks", },
2195 {"f_bfree", },
2196 {"f_bavail", },
2197 {"f_files", },
2198 {"f_ffree", },
2199 {"f_favail", },
2200 {"f_flag", },
2201 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002202 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002203 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002204};
2205
2206static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002207 "statvfs_result", /* name */
2208 statvfs_result__doc__, /* doc */
2209 statvfs_result_fields,
2210 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002211};
2212
Ross Lagerwall7807c352011-03-17 20:20:30 +02002213#if defined(HAVE_WAITID) && !defined(__APPLE__)
2214PyDoc_STRVAR(waitid_result__doc__,
2215"waitid_result: Result from waitid.\n\n\
2216This object may be accessed either as a tuple of\n\
2217 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2218or via the attributes si_pid, si_uid, and so on.\n\
2219\n\
2220See os.waitid for more information.");
2221
2222static PyStructSequence_Field waitid_result_fields[] = {
2223 {"si_pid", },
2224 {"si_uid", },
2225 {"si_signo", },
2226 {"si_status", },
2227 {"si_code", },
2228 {0}
2229};
2230
2231static PyStructSequence_Desc waitid_result_desc = {
2232 "waitid_result", /* name */
2233 waitid_result__doc__, /* doc */
2234 waitid_result_fields,
2235 5
2236};
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002237#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002238static newfunc structseq_new;
2239
2240static PyObject *
2241statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2242{
Victor Stinner8c62be82010-05-06 00:08:46 +00002243 PyStructSequence *result;
2244 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002245
Victor Stinner8c62be82010-05-06 00:08:46 +00002246 result = (PyStructSequence*)structseq_new(type, args, kwds);
2247 if (!result)
2248 return NULL;
2249 /* If we have been initialized from a tuple,
2250 st_?time might be set to None. Initialize it
2251 from the int slots. */
2252 for (i = 7; i <= 9; i++) {
2253 if (result->ob_item[i+3] == Py_None) {
2254 Py_DECREF(Py_None);
2255 Py_INCREF(result->ob_item[i]);
2256 result->ob_item[i+3] = result->ob_item[i];
2257 }
2258 }
2259 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002260}
2261
Eddie Elizondob3966632019-11-05 07:16:14 -08002262static int
2263_posix_clear(PyObject *module)
2264{
Victor Stinner97f33c32020-05-14 18:05:58 +02002265 _posixstate *state = get_posix_state(module);
2266 Py_CLEAR(state->billion);
2267 Py_CLEAR(state->DirEntryType);
2268 Py_CLEAR(state->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002269#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Victor Stinner97f33c32020-05-14 18:05:58 +02002270 Py_CLEAR(state->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002271#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002272 Py_CLEAR(state->StatResultType);
2273 Py_CLEAR(state->StatVFSResultType);
2274 Py_CLEAR(state->TerminalSizeType);
2275 Py_CLEAR(state->TimesResultType);
2276 Py_CLEAR(state->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002277#if defined(HAVE_WAITID) && !defined(__APPLE__)
Victor Stinner97f33c32020-05-14 18:05:58 +02002278 Py_CLEAR(state->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002279#endif
2280#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Victor Stinner97f33c32020-05-14 18:05:58 +02002281 Py_CLEAR(state->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002282#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002283 Py_CLEAR(state->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002284 return 0;
2285}
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002286
Eddie Elizondob3966632019-11-05 07:16:14 -08002287static int
2288_posix_traverse(PyObject *module, visitproc visit, void *arg)
2289{
Victor Stinner97f33c32020-05-14 18:05:58 +02002290 _posixstate *state = get_posix_state(module);
2291 Py_VISIT(state->billion);
2292 Py_VISIT(state->DirEntryType);
2293 Py_VISIT(state->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002294#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Victor Stinner97f33c32020-05-14 18:05:58 +02002295 Py_VISIT(state->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002296#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002297 Py_VISIT(state->StatResultType);
2298 Py_VISIT(state->StatVFSResultType);
2299 Py_VISIT(state->TerminalSizeType);
2300 Py_VISIT(state->TimesResultType);
2301 Py_VISIT(state->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002302#if defined(HAVE_WAITID) && !defined(__APPLE__)
Victor Stinner97f33c32020-05-14 18:05:58 +02002303 Py_VISIT(state->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002304#endif
2305#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Victor Stinner97f33c32020-05-14 18:05:58 +02002306 Py_VISIT(state->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002307#endif
Victor Stinner97f33c32020-05-14 18:05:58 +02002308 Py_VISIT(state->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002309 return 0;
2310}
2311
2312static void
2313_posix_free(void *module)
2314{
2315 _posix_clear((PyObject *)module);
2316}
Larry Hastings6fe20b32012-04-19 15:07:49 -07002317
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002318static void
Victor Stinner1c2fa782020-05-10 11:05:29 +02002319fill_time(PyObject *module, PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002320{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002321 PyObject *s = _PyLong_FromTime_t(sec);
2322 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2323 PyObject *s_in_ns = NULL;
2324 PyObject *ns_total = NULL;
2325 PyObject *float_s = NULL;
2326
2327 if (!(s && ns_fractional))
2328 goto exit;
2329
Victor Stinner1c2fa782020-05-10 11:05:29 +02002330 s_in_ns = PyNumber_Multiply(s, get_posix_state(module)->billion);
Larry Hastings6fe20b32012-04-19 15:07:49 -07002331 if (!s_in_ns)
2332 goto exit;
2333
2334 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2335 if (!ns_total)
2336 goto exit;
2337
Victor Stinner01b5aab2017-10-24 02:02:00 -07002338 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2339 if (!float_s) {
2340 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002341 }
2342
2343 PyStructSequence_SET_ITEM(v, index, s);
2344 PyStructSequence_SET_ITEM(v, index+3, float_s);
2345 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2346 s = NULL;
2347 float_s = NULL;
2348 ns_total = NULL;
2349exit:
2350 Py_XDECREF(s);
2351 Py_XDECREF(ns_fractional);
2352 Py_XDECREF(s_in_ns);
2353 Py_XDECREF(ns_total);
2354 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002355}
2356
Tim Peters5aa91602002-01-30 05:46:57 +00002357/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002358 (used by posix_stat() and posix_fstat()) */
2359static PyObject*
Victor Stinner1c2fa782020-05-10 11:05:29 +02002360_pystat_fromstructstat(PyObject *module, STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002361{
Victor Stinner8c62be82010-05-06 00:08:46 +00002362 unsigned long ansec, mnsec, cnsec;
Victor Stinner1c2fa782020-05-10 11:05:29 +02002363 PyObject *StatResultType = get_posix_state(module)->StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08002364 PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002365 if (v == NULL)
2366 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002367
Victor Stinner8c62be82010-05-06 00:08:46 +00002368 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002369 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002370 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002371#ifdef MS_WINDOWS
2372 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002373#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002374 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002375#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002376 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002377#if defined(MS_WINDOWS)
2378 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2379 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2380#else
2381 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2382 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2383#endif
xdegaye50e86032017-05-22 11:15:08 +02002384 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2385 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002386
Martin v. Löwis14694662006-02-03 12:54:16 +00002387#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002388 ansec = st->st_atim.tv_nsec;
2389 mnsec = st->st_mtim.tv_nsec;
2390 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002391#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002392 ansec = st->st_atimespec.tv_nsec;
2393 mnsec = st->st_mtimespec.tv_nsec;
2394 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002395#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002396 ansec = st->st_atime_nsec;
2397 mnsec = st->st_mtime_nsec;
2398 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002399#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002400 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002401#endif
Victor Stinner1c2fa782020-05-10 11:05:29 +02002402 fill_time(module, v, 7, st->st_atime, ansec);
2403 fill_time(module, v, 8, st->st_mtime, mnsec);
2404 fill_time(module, v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002405
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002406#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002407 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2408 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002409#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002410#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002411 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2412 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002413#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002414#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002415 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2416 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002417#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002418#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002419 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2420 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002421#endif
2422#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002423 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002424 PyObject *val;
2425 unsigned long bsec,bnsec;
2426 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002427#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002428 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002429#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002430 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002431#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002432 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002433 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2434 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002435 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002436#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002437#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002438 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2439 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002440#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002441#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2442 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2443 PyLong_FromUnsignedLong(st->st_file_attributes));
2444#endif
jcea6c51d512018-01-28 14:00:08 +01002445#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2446 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2447 PyUnicode_FromString(st->st_fstype));
2448#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002449#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2450 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2451 PyLong_FromUnsignedLong(st->st_reparse_tag));
2452#endif
Fred Drake699f3522000-06-29 21:12:41 +00002453
Victor Stinner8c62be82010-05-06 00:08:46 +00002454 if (PyErr_Occurred()) {
2455 Py_DECREF(v);
2456 return NULL;
2457 }
Fred Drake699f3522000-06-29 21:12:41 +00002458
Victor Stinner8c62be82010-05-06 00:08:46 +00002459 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002460}
2461
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002462/* POSIX methods */
2463
Guido van Rossum94f6f721999-01-06 18:42:14 +00002464
2465static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02002466posix_do_stat(PyObject *module, const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002467 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002468{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002469 STRUCT_STAT st;
2470 int result;
2471
Ronald Oussoren41761932020-11-08 10:05:27 +01002472#ifdef HAVE_FSTATAT
2473 int fstatat_unavailable = 0;
2474#endif
2475
Larry Hastings9cf065c2012-06-22 16:30:09 -07002476#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2477 if (follow_symlinks_specified(function_name, follow_symlinks))
2478 return NULL;
2479#endif
2480
2481 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2482 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2483 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2484 return NULL;
2485
2486 Py_BEGIN_ALLOW_THREADS
2487 if (path->fd != -1)
2488 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002489#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002490 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002491 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002492 else
Steve Dowercc16be82016-09-08 10:35:16 -07002493 result = win32_lstat(path->wide, &st);
2494#else
2495 else
2496#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002497 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2498 result = LSTAT(path->narrow, &st);
2499 else
Steve Dowercc16be82016-09-08 10:35:16 -07002500#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002501#ifdef HAVE_FSTATAT
Ronald Oussoren41761932020-11-08 10:05:27 +01002502 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
2503 if (HAVE_FSTATAT_RUNTIME) {
2504 result = fstatat(dir_fd, path->narrow, &st,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002505 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
Ronald Oussoren41761932020-11-08 10:05:27 +01002506
2507 } else {
2508 fstatat_unavailable = 1;
2509 }
2510 } else
Steve Dowercc16be82016-09-08 10:35:16 -07002511#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002512 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002513#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002514 Py_END_ALLOW_THREADS
2515
Ronald Oussoren41761932020-11-08 10:05:27 +01002516#ifdef HAVE_FSTATAT
2517 if (fstatat_unavailable) {
2518 argument_unavailable_error("stat", "dir_fd");
2519 return NULL;
2520 }
2521#endif
2522
Victor Stinner292c8352012-10-30 02:17:38 +01002523 if (result != 0) {
2524 return path_error(path);
2525 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002526
Victor Stinner1c2fa782020-05-10 11:05:29 +02002527 return _pystat_fromstructstat(module, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002528}
2529
Larry Hastings2f936352014-08-05 14:04:04 +10002530/*[python input]
2531
2532for s in """
2533
2534FACCESSAT
2535FCHMODAT
2536FCHOWNAT
2537FSTATAT
2538LINKAT
2539MKDIRAT
2540MKFIFOAT
2541MKNODAT
2542OPENAT
2543READLINKAT
2544SYMLINKAT
2545UNLINKAT
2546
2547""".strip().split():
2548 s = s.strip()
2549 print("""
2550#ifdef HAVE_{s}
2551 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002552#else
Larry Hastings2f936352014-08-05 14:04:04 +10002553 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002554#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002555""".rstrip().format(s=s))
2556
2557for s in """
2558
2559FCHDIR
2560FCHMOD
2561FCHOWN
2562FDOPENDIR
2563FEXECVE
2564FPATHCONF
2565FSTATVFS
2566FTRUNCATE
2567
2568""".strip().split():
2569 s = s.strip()
2570 print("""
2571#ifdef HAVE_{s}
2572 #define PATH_HAVE_{s} 1
2573#else
2574 #define PATH_HAVE_{s} 0
2575#endif
2576
2577""".rstrip().format(s=s))
2578[python start generated code]*/
2579
2580#ifdef HAVE_FACCESSAT
2581 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2582#else
2583 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2584#endif
2585
2586#ifdef HAVE_FCHMODAT
2587 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2588#else
2589 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2590#endif
2591
2592#ifdef HAVE_FCHOWNAT
2593 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2594#else
2595 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2596#endif
2597
2598#ifdef HAVE_FSTATAT
2599 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2600#else
2601 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2602#endif
2603
2604#ifdef HAVE_LINKAT
2605 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2606#else
2607 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2608#endif
2609
2610#ifdef HAVE_MKDIRAT
2611 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2612#else
2613 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2614#endif
2615
2616#ifdef HAVE_MKFIFOAT
2617 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2618#else
2619 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2620#endif
2621
2622#ifdef HAVE_MKNODAT
2623 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2624#else
2625 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2626#endif
2627
2628#ifdef HAVE_OPENAT
2629 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2630#else
2631 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2632#endif
2633
2634#ifdef HAVE_READLINKAT
2635 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2636#else
2637 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2638#endif
2639
2640#ifdef HAVE_SYMLINKAT
2641 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2642#else
2643 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2644#endif
2645
2646#ifdef HAVE_UNLINKAT
2647 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2648#else
2649 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2650#endif
2651
2652#ifdef HAVE_FCHDIR
2653 #define PATH_HAVE_FCHDIR 1
2654#else
2655 #define PATH_HAVE_FCHDIR 0
2656#endif
2657
2658#ifdef HAVE_FCHMOD
2659 #define PATH_HAVE_FCHMOD 1
2660#else
2661 #define PATH_HAVE_FCHMOD 0
2662#endif
2663
2664#ifdef HAVE_FCHOWN
2665 #define PATH_HAVE_FCHOWN 1
2666#else
2667 #define PATH_HAVE_FCHOWN 0
2668#endif
2669
2670#ifdef HAVE_FDOPENDIR
2671 #define PATH_HAVE_FDOPENDIR 1
2672#else
2673 #define PATH_HAVE_FDOPENDIR 0
2674#endif
2675
2676#ifdef HAVE_FEXECVE
2677 #define PATH_HAVE_FEXECVE 1
2678#else
2679 #define PATH_HAVE_FEXECVE 0
2680#endif
2681
2682#ifdef HAVE_FPATHCONF
2683 #define PATH_HAVE_FPATHCONF 1
2684#else
2685 #define PATH_HAVE_FPATHCONF 0
2686#endif
2687
2688#ifdef HAVE_FSTATVFS
2689 #define PATH_HAVE_FSTATVFS 1
2690#else
2691 #define PATH_HAVE_FSTATVFS 0
2692#endif
2693
2694#ifdef HAVE_FTRUNCATE
2695 #define PATH_HAVE_FTRUNCATE 1
2696#else
2697 #define PATH_HAVE_FTRUNCATE 0
2698#endif
2699/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002700
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002701#ifdef MS_WINDOWS
2702 #undef PATH_HAVE_FTRUNCATE
2703 #define PATH_HAVE_FTRUNCATE 1
2704#endif
Larry Hastings31826802013-10-19 00:09:25 -07002705
Larry Hastings61272b72014-01-07 12:41:53 -08002706/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002707
2708class path_t_converter(CConverter):
2709
2710 type = "path_t"
2711 impl_by_reference = True
2712 parse_by_reference = True
2713
2714 converter = 'path_converter'
2715
2716 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002717 # right now path_t doesn't support default values.
2718 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002719 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002720 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002721
Larry Hastings2f936352014-08-05 14:04:04 +10002722 if self.c_default not in (None, 'Py_None'):
2723 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002724
2725 self.nullable = nullable
2726 self.allow_fd = allow_fd
2727
Larry Hastings7726ac92014-01-31 22:03:12 -08002728 def pre_render(self):
2729 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002730 if isinstance(value, str):
2731 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002732 return str(int(bool(value)))
2733
2734 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002735 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002736 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002737 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002738 strify(self.nullable),
2739 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002740 )
2741
2742 def cleanup(self):
2743 return "path_cleanup(&" + self.name + ");\n"
2744
2745
2746class dir_fd_converter(CConverter):
2747 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002748
Larry Hastings2f936352014-08-05 14:04:04 +10002749 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002750 if self.default in (unspecified, None):
2751 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002752 if isinstance(requires, str):
2753 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2754 else:
2755 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002756
Larry Hastings2f936352014-08-05 14:04:04 +10002757class uid_t_converter(CConverter):
2758 type = "uid_t"
2759 converter = '_Py_Uid_Converter'
2760
2761class gid_t_converter(CConverter):
2762 type = "gid_t"
2763 converter = '_Py_Gid_Converter'
2764
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002765class dev_t_converter(CConverter):
2766 type = 'dev_t'
2767 converter = '_Py_Dev_Converter'
2768
2769class dev_t_return_converter(unsigned_long_return_converter):
2770 type = 'dev_t'
2771 conversion_fn = '_PyLong_FromDev'
2772 unsigned_cast = '(dev_t)'
2773
Larry Hastings2f936352014-08-05 14:04:04 +10002774class FSConverter_converter(CConverter):
2775 type = 'PyObject *'
2776 converter = 'PyUnicode_FSConverter'
2777 def converter_init(self):
2778 if self.default is not unspecified:
2779 fail("FSConverter_converter does not support default values")
2780 self.c_default = 'NULL'
2781
2782 def cleanup(self):
2783 return "Py_XDECREF(" + self.name + ");\n"
2784
2785class pid_t_converter(CConverter):
2786 type = 'pid_t'
2787 format_unit = '" _Py_PARSE_PID "'
2788
2789class idtype_t_converter(int_converter):
2790 type = 'idtype_t'
2791
2792class id_t_converter(CConverter):
2793 type = 'id_t'
2794 format_unit = '" _Py_PARSE_PID "'
2795
Benjamin Petersonca470632016-09-06 13:47:26 -07002796class intptr_t_converter(CConverter):
2797 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002798 format_unit = '" _Py_PARSE_INTPTR "'
2799
2800class Py_off_t_converter(CConverter):
2801 type = 'Py_off_t'
2802 converter = 'Py_off_t_converter'
2803
2804class Py_off_t_return_converter(long_return_converter):
2805 type = 'Py_off_t'
2806 conversion_fn = 'PyLong_FromPy_off_t'
2807
2808class path_confname_converter(CConverter):
2809 type="int"
2810 converter="conv_path_confname"
2811
2812class confstr_confname_converter(path_confname_converter):
2813 converter='conv_confstr_confname'
2814
2815class sysconf_confname_converter(path_confname_converter):
2816 converter="conv_sysconf_confname"
2817
Larry Hastings61272b72014-01-07 12:41:53 -08002818[python start generated code]*/
Serhiy Storchaka9975cc52020-10-09 23:00:45 +03002819/*[python end generated code: output=da39a3ee5e6b4b0d input=3338733161aa7879]*/
Larry Hastings31826802013-10-19 00:09:25 -07002820
Larry Hastings61272b72014-01-07 12:41:53 -08002821/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002822
Larry Hastings2a727912014-01-16 11:32:01 -08002823os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002824
2825 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002826 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002827 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002828
2829 *
2830
Larry Hastings2f936352014-08-05 14:04:04 +10002831 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002832 If not None, it should be a file descriptor open to a directory,
2833 and path should be a relative string; path will then be relative to
2834 that directory.
2835
2836 follow_symlinks: bool = True
2837 If False, and the last element of the path is a symbolic link,
2838 stat will examine the symbolic link itself instead of the file
2839 the link points to.
2840
2841Perform a stat system call on the given path.
2842
2843dir_fd and follow_symlinks may not be implemented
2844 on your platform. If they are unavailable, using them will raise a
2845 NotImplementedError.
2846
2847It's an error to use dir_fd or follow_symlinks when specifying path as
2848 an open file descriptor.
2849
Larry Hastings61272b72014-01-07 12:41:53 -08002850[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002851
Larry Hastings31826802013-10-19 00:09:25 -07002852static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002853os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002854/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002855{
Victor Stinner1c2fa782020-05-10 11:05:29 +02002856 return posix_do_stat(module, "stat", path, dir_fd, follow_symlinks);
Larry Hastings31826802013-10-19 00:09:25 -07002857}
2858
Larry Hastings2f936352014-08-05 14:04:04 +10002859
2860/*[clinic input]
2861os.lstat
2862
2863 path : path_t
2864
2865 *
2866
2867 dir_fd : dir_fd(requires='fstatat') = None
2868
2869Perform a stat system call on the given path, without following symbolic links.
2870
2871Like stat(), but do not follow symbolic links.
2872Equivalent to stat(path, follow_symlinks=False).
2873[clinic start generated code]*/
2874
Larry Hastings2f936352014-08-05 14:04:04 +10002875static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002876os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2877/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002878{
2879 int follow_symlinks = 0;
Victor Stinner1c2fa782020-05-10 11:05:29 +02002880 return posix_do_stat(module, "lstat", path, dir_fd, follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10002881}
Larry Hastings31826802013-10-19 00:09:25 -07002882
Larry Hastings2f936352014-08-05 14:04:04 +10002883
Larry Hastings61272b72014-01-07 12:41:53 -08002884/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002885os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002886
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002887 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002888 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002889
2890 mode: int
2891 Operating-system mode bitfield. Can be F_OK to test existence,
2892 or the inclusive-OR of R_OK, W_OK, and X_OK.
2893
2894 *
2895
Larry Hastings2f936352014-08-05 14:04:04 +10002896 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002897 If not None, it should be a file descriptor open to a directory,
2898 and path should be relative; path will then be relative to that
2899 directory.
2900
2901 effective_ids: bool = False
2902 If True, access will use the effective uid/gid instead of
2903 the real uid/gid.
2904
2905 follow_symlinks: bool = True
2906 If False, and the last element of the path is a symbolic link,
2907 access will examine the symbolic link itself instead of the file
2908 the link points to.
2909
2910Use the real uid/gid to test for access to a path.
2911
2912{parameters}
2913dir_fd, effective_ids, and follow_symlinks may not be implemented
2914 on your platform. If they are unavailable, using them will raise a
2915 NotImplementedError.
2916
2917Note that most operations will use the effective uid/gid, therefore this
2918 routine can be used in a suid/sgid environment to test if the invoking user
2919 has the specified access to the path.
2920
Larry Hastings61272b72014-01-07 12:41:53 -08002921[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002922
Larry Hastings2f936352014-08-05 14:04:04 +10002923static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002924os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002925 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002926/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002927{
Larry Hastings2f936352014-08-05 14:04:04 +10002928 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002929
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002930#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002931 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002932#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002933 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002934#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002935
Ronald Oussoren41761932020-11-08 10:05:27 +01002936#ifdef HAVE_FACCESSAT
2937 int faccessat_unavailable = 0;
2938#endif
2939
Larry Hastings9cf065c2012-06-22 16:30:09 -07002940#ifndef HAVE_FACCESSAT
2941 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002942 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002943
2944 if (effective_ids) {
2945 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002946 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002947 }
2948#endif
2949
2950#ifdef MS_WINDOWS
2951 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002952 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002953 Py_END_ALLOW_THREADS
2954
2955 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002956 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002957 * * we didn't get a -1, and
2958 * * write access wasn't requested,
2959 * * or the file isn't read-only,
2960 * * or it's a directory.
2961 * (Directories cannot be read-only on Windows.)
2962 */
Larry Hastings2f936352014-08-05 14:04:04 +10002963 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002964 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002965 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002966 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002967#else
2968
2969 Py_BEGIN_ALLOW_THREADS
2970#ifdef HAVE_FACCESSAT
2971 if ((dir_fd != DEFAULT_DIR_FD) ||
2972 effective_ids ||
2973 !follow_symlinks) {
Ronald Oussoren41761932020-11-08 10:05:27 +01002974
2975 if (HAVE_FACCESSAT_RUNTIME) {
2976 int flags = 0;
2977 if (!follow_symlinks)
2978 flags |= AT_SYMLINK_NOFOLLOW;
2979 if (effective_ids)
2980 flags |= AT_EACCESS;
2981 result = faccessat(dir_fd, path->narrow, mode, flags);
2982 } else {
2983 faccessat_unavailable = 1;
2984 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002985 }
2986 else
2987#endif
Larry Hastings31826802013-10-19 00:09:25 -07002988 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002989 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01002990
2991#ifdef HAVE_FACCESSAT
2992 if (faccessat_unavailable) {
2993 if (dir_fd != DEFAULT_DIR_FD) {
2994 argument_unavailable_error("access", "dir_fd");
2995 return -1;
2996 }
2997 if (follow_symlinks_specified("access", follow_symlinks))
2998 return -1;
2999
3000 if (effective_ids) {
3001 argument_unavailable_error("access", "effective_ids");
3002 return -1;
3003 }
3004 /* should be unreachable */
3005 return -1;
3006 }
3007#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003008 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003009#endif
3010
Larry Hastings9cf065c2012-06-22 16:30:09 -07003011 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003012}
3013
Guido van Rossumd371ff11999-01-25 16:12:23 +00003014#ifndef F_OK
3015#define F_OK 0
3016#endif
3017#ifndef R_OK
3018#define R_OK 4
3019#endif
3020#ifndef W_OK
3021#define W_OK 2
3022#endif
3023#ifndef X_OK
3024#define X_OK 1
3025#endif
3026
Larry Hastings31826802013-10-19 00:09:25 -07003027
Guido van Rossumd371ff11999-01-25 16:12:23 +00003028#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08003029/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003030os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07003031
3032 fd: int
3033 Integer file descriptor handle.
3034
3035 /
3036
3037Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08003038[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07003039
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003040static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003041os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003042/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07003043{
Guido van Rossum94f6f721999-01-06 18:42:14 +00003044
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02003045 long size = sysconf(_SC_TTY_NAME_MAX);
3046 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02003047 return posix_error();
3048 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02003049 char *buffer = (char *)PyMem_RawMalloc(size);
3050 if (buffer == NULL) {
3051 return PyErr_NoMemory();
3052 }
3053 int ret = ttyname_r(fd, buffer, size);
3054 if (ret != 0) {
3055 PyMem_RawFree(buffer);
3056 errno = ret;
3057 return posix_error();
3058 }
3059 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
3060 PyMem_RawFree(buffer);
3061 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00003062}
Guido van Rossumd371ff11999-01-25 16:12:23 +00003063#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00003064
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003065#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10003066/*[clinic input]
3067os.ctermid
3068
3069Return the name of the controlling terminal for this process.
3070[clinic start generated code]*/
3071
Larry Hastings2f936352014-08-05 14:04:04 +10003072static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003073os_ctermid_impl(PyObject *module)
3074/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003075{
Victor Stinner8c62be82010-05-06 00:08:46 +00003076 char *ret;
3077 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003078
Greg Wardb48bc172000-03-01 21:51:56 +00003079#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00003080 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003081#else
Victor Stinner8c62be82010-05-06 00:08:46 +00003082 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003083#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00003084 if (ret == NULL)
3085 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00003086 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003087}
Larry Hastings2f936352014-08-05 14:04:04 +10003088#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00003089
Larry Hastings2f936352014-08-05 14:04:04 +10003090
3091/*[clinic input]
3092os.chdir
3093
3094 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
3095
3096Change the current working directory to the specified path.
3097
3098path may always be specified as a string.
3099On some platforms, path may also be specified as an open file descriptor.
3100 If this functionality is unavailable, using it raises an exception.
3101[clinic start generated code]*/
3102
Larry Hastings2f936352014-08-05 14:04:04 +10003103static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003104os_chdir_impl(PyObject *module, path_t *path)
3105/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003106{
3107 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003108
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003109 if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
3110 return NULL;
3111 }
3112
Larry Hastings9cf065c2012-06-22 16:30:09 -07003113 Py_BEGIN_ALLOW_THREADS
3114#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003115 /* on unix, success = 0, on windows, success = !0 */
3116 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003117#else
3118#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10003119 if (path->fd != -1)
3120 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003121 else
3122#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003123 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003124#endif
3125 Py_END_ALLOW_THREADS
3126
3127 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003128 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003129 }
3130
Larry Hastings2f936352014-08-05 14:04:04 +10003131 Py_RETURN_NONE;
3132}
3133
3134
3135#ifdef HAVE_FCHDIR
3136/*[clinic input]
3137os.fchdir
3138
3139 fd: fildes
3140
3141Change to the directory of the given file descriptor.
3142
3143fd must be opened on a directory, not a file.
3144Equivalent to os.chdir(fd).
3145
3146[clinic start generated code]*/
3147
Fred Drake4d1e64b2002-04-15 19:40:07 +00003148static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003149os_fchdir_impl(PyObject *module, int fd)
3150/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00003151{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003152 if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
3153 return NULL;
3154 }
Larry Hastings2f936352014-08-05 14:04:04 +10003155 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00003156}
3157#endif /* HAVE_FCHDIR */
3158
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003159
Larry Hastings2f936352014-08-05 14:04:04 +10003160/*[clinic input]
3161os.chmod
3162
3163 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00003164 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10003165 On some platforms, path may also be specified as an open file descriptor.
3166 If this functionality is unavailable, using it raises an exception.
3167
3168 mode: int
3169 Operating-system mode bitfield.
3170
3171 *
3172
3173 dir_fd : dir_fd(requires='fchmodat') = None
3174 If not None, it should be a file descriptor open to a directory,
3175 and path should be relative; path will then be relative to that
3176 directory.
3177
3178 follow_symlinks: bool = True
3179 If False, and the last element of the path is a symbolic link,
3180 chmod will modify the symbolic link itself instead of the file
3181 the link points to.
3182
3183Change the access permissions of a file.
3184
3185It is an error to use dir_fd or follow_symlinks when specifying path as
3186 an open file descriptor.
3187dir_fd and follow_symlinks may not be implemented on your platform.
3188 If they are unavailable, using them will raise a NotImplementedError.
3189
3190[clinic start generated code]*/
3191
Larry Hastings2f936352014-08-05 14:04:04 +10003192static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003193os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003194 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003195/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003196{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003197 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003198
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003199#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003200 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003201#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003202
Larry Hastings9cf065c2012-06-22 16:30:09 -07003203#ifdef HAVE_FCHMODAT
3204 int fchmodat_nofollow_unsupported = 0;
Ronald Oussoren41761932020-11-08 10:05:27 +01003205 int fchmodat_unsupported = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003206#endif
3207
Larry Hastings9cf065c2012-06-22 16:30:09 -07003208#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
3209 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003210 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003211#endif
3212
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003213 if (PySys_Audit("os.chmod", "Oii", path->object, mode,
3214 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3215 return NULL;
3216 }
3217
Larry Hastings9cf065c2012-06-22 16:30:09 -07003218#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003219 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003220 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01003221 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003222 result = 0;
3223 else {
3224 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00003225 attr &= ~FILE_ATTRIBUTE_READONLY;
3226 else
3227 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07003228 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003229 }
3230 Py_END_ALLOW_THREADS
3231
3232 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003233 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003234 }
3235#else /* MS_WINDOWS */
3236 Py_BEGIN_ALLOW_THREADS
3237#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003238 if (path->fd != -1)
3239 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003240 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003241#endif /* HAVE_CHMOD */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003242#ifdef HAVE_LCHMOD
3243 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003244 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003245 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003246#endif /* HAVE_LCHMOD */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003247#ifdef HAVE_FCHMODAT
3248 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
Ronald Oussoren41761932020-11-08 10:05:27 +01003249 if (HAVE_FCHMODAT_RUNTIME) {
3250 /*
3251 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3252 * The documentation specifically shows how to use it,
3253 * and then says it isn't implemented yet.
3254 * (true on linux with glibc 2.15, and openindiana 3.x)
3255 *
3256 * Once it is supported, os.chmod will automatically
3257 * support dir_fd and follow_symlinks=False. (Hopefully.)
3258 * Until then, we need to be careful what exception we raise.
3259 */
3260 result = fchmodat(dir_fd, path->narrow, mode,
3261 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3262 /*
3263 * But wait! We can't throw the exception without allowing threads,
3264 * and we can't do that in this nested scope. (Macro trickery, sigh.)
3265 */
3266 fchmodat_nofollow_unsupported =
3267 result &&
3268 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3269 !follow_symlinks;
3270 } else {
3271 fchmodat_unsupported = 1;
3272 fchmodat_nofollow_unsupported = 1;
3273
3274 result = -1;
3275 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003276 }
3277 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003278#endif /* HAVE_FHCMODAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003279 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003280 Py_END_ALLOW_THREADS
3281
3282 if (result) {
3283#ifdef HAVE_FCHMODAT
Ronald Oussoren41761932020-11-08 10:05:27 +01003284 if (fchmodat_unsupported) {
3285 if (dir_fd != DEFAULT_DIR_FD) {
3286 argument_unavailable_error("chmod", "dir_fd");
3287 return NULL;
3288 }
3289 }
3290
Larry Hastings9cf065c2012-06-22 16:30:09 -07003291 if (fchmodat_nofollow_unsupported) {
3292 if (dir_fd != DEFAULT_DIR_FD)
3293 dir_fd_and_follow_symlinks_invalid("chmod",
3294 dir_fd, follow_symlinks);
3295 else
3296 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003297 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003298 }
3299 else
Ronald Oussoren41761932020-11-08 10:05:27 +01003300#endif /* HAVE_FCHMODAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003301 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003302 }
Ronald Oussoren41761932020-11-08 10:05:27 +01003303#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003304
Larry Hastings2f936352014-08-05 14:04:04 +10003305 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003306}
3307
Larry Hastings9cf065c2012-06-22 16:30:09 -07003308
Christian Heimes4e30a842007-11-30 22:12:06 +00003309#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003310/*[clinic input]
3311os.fchmod
3312
3313 fd: int
3314 mode: int
3315
3316Change the access permissions of the file given by file descriptor fd.
3317
3318Equivalent to os.chmod(fd, mode).
3319[clinic start generated code]*/
3320
Larry Hastings2f936352014-08-05 14:04:04 +10003321static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003322os_fchmod_impl(PyObject *module, int fd, int mode)
3323/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003324{
3325 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003326 int async_err = 0;
3327
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003328 if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
3329 return NULL;
3330 }
3331
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003332 do {
3333 Py_BEGIN_ALLOW_THREADS
3334 res = fchmod(fd, mode);
3335 Py_END_ALLOW_THREADS
3336 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3337 if (res != 0)
3338 return (!async_err) ? posix_error() : NULL;
3339
Victor Stinner8c62be82010-05-06 00:08:46 +00003340 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003341}
3342#endif /* HAVE_FCHMOD */
3343
Larry Hastings2f936352014-08-05 14:04:04 +10003344
Christian Heimes4e30a842007-11-30 22:12:06 +00003345#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003346/*[clinic input]
3347os.lchmod
3348
3349 path: path_t
3350 mode: int
3351
3352Change the access permissions of a file, without following symbolic links.
3353
3354If path is a symlink, this affects the link itself rather than the target.
3355Equivalent to chmod(path, mode, follow_symlinks=False)."
3356[clinic start generated code]*/
3357
Larry Hastings2f936352014-08-05 14:04:04 +10003358static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003359os_lchmod_impl(PyObject *module, path_t *path, int mode)
3360/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003361{
Victor Stinner8c62be82010-05-06 00:08:46 +00003362 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003363 if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
3364 return NULL;
3365 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003366 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003367 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003368 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003369 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003370 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003371 return NULL;
3372 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003373 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003374}
3375#endif /* HAVE_LCHMOD */
3376
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003377
Thomas Wouterscf297e42007-02-23 15:07:44 +00003378#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003379/*[clinic input]
3380os.chflags
3381
3382 path: path_t
3383 flags: unsigned_long(bitwise=True)
3384 follow_symlinks: bool=True
3385
3386Set file flags.
3387
3388If follow_symlinks is False, and the last element of the path is a symbolic
3389 link, chflags will change flags on the symbolic link itself instead of the
3390 file the link points to.
3391follow_symlinks may not be implemented on your platform. If it is
3392unavailable, using it will raise a NotImplementedError.
3393
3394[clinic start generated code]*/
3395
Larry Hastings2f936352014-08-05 14:04:04 +10003396static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003397os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003398 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003399/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003400{
3401 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003402
3403#ifndef HAVE_LCHFLAGS
3404 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003405 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003406#endif
3407
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003408 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3409 return NULL;
3410 }
3411
Victor Stinner8c62be82010-05-06 00:08:46 +00003412 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003413#ifdef HAVE_LCHFLAGS
3414 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003415 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003416 else
3417#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003418 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003419 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003420
Larry Hastings2f936352014-08-05 14:04:04 +10003421 if (result)
3422 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003423
Larry Hastings2f936352014-08-05 14:04:04 +10003424 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003425}
3426#endif /* HAVE_CHFLAGS */
3427
Larry Hastings2f936352014-08-05 14:04:04 +10003428
Thomas Wouterscf297e42007-02-23 15:07:44 +00003429#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003430/*[clinic input]
3431os.lchflags
3432
3433 path: path_t
3434 flags: unsigned_long(bitwise=True)
3435
3436Set file flags.
3437
3438This function will not follow symbolic links.
3439Equivalent to chflags(path, flags, follow_symlinks=False).
3440[clinic start generated code]*/
3441
Larry Hastings2f936352014-08-05 14:04:04 +10003442static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003443os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3444/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003445{
Victor Stinner8c62be82010-05-06 00:08:46 +00003446 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003447 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3448 return NULL;
3449 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003450 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003451 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003452 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003453 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003454 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003455 }
Victor Stinner292c8352012-10-30 02:17:38 +01003456 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003457}
3458#endif /* HAVE_LCHFLAGS */
3459
Larry Hastings2f936352014-08-05 14:04:04 +10003460
Martin v. Löwis244edc82001-10-04 22:44:26 +00003461#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003462/*[clinic input]
3463os.chroot
3464 path: path_t
3465
3466Change root directory to path.
3467
3468[clinic start generated code]*/
3469
Larry Hastings2f936352014-08-05 14:04:04 +10003470static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003471os_chroot_impl(PyObject *module, path_t *path)
3472/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003473{
3474 int res;
3475 Py_BEGIN_ALLOW_THREADS
3476 res = chroot(path->narrow);
3477 Py_END_ALLOW_THREADS
3478 if (res < 0)
3479 return path_error(path);
3480 Py_RETURN_NONE;
3481}
3482#endif /* HAVE_CHROOT */
3483
Martin v. Löwis244edc82001-10-04 22:44:26 +00003484
Guido van Rossum21142a01999-01-08 21:05:37 +00003485#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003486/*[clinic input]
3487os.fsync
3488
3489 fd: fildes
3490
3491Force write of fd to disk.
3492[clinic start generated code]*/
3493
Larry Hastings2f936352014-08-05 14:04:04 +10003494static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003495os_fsync_impl(PyObject *module, int fd)
3496/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003497{
3498 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003499}
3500#endif /* HAVE_FSYNC */
3501
Larry Hastings2f936352014-08-05 14:04:04 +10003502
Ross Lagerwall7807c352011-03-17 20:20:30 +02003503#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003504/*[clinic input]
3505os.sync
3506
3507Force write of everything to disk.
3508[clinic start generated code]*/
3509
Larry Hastings2f936352014-08-05 14:04:04 +10003510static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003511os_sync_impl(PyObject *module)
3512/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003513{
3514 Py_BEGIN_ALLOW_THREADS
3515 sync();
3516 Py_END_ALLOW_THREADS
3517 Py_RETURN_NONE;
3518}
Larry Hastings2f936352014-08-05 14:04:04 +10003519#endif /* HAVE_SYNC */
3520
Ross Lagerwall7807c352011-03-17 20:20:30 +02003521
Guido van Rossum21142a01999-01-08 21:05:37 +00003522#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003523#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003524extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3525#endif
3526
Larry Hastings2f936352014-08-05 14:04:04 +10003527/*[clinic input]
3528os.fdatasync
3529
3530 fd: fildes
3531
3532Force write of fd to disk without forcing update of metadata.
3533[clinic start generated code]*/
3534
Larry Hastings2f936352014-08-05 14:04:04 +10003535static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003536os_fdatasync_impl(PyObject *module, int fd)
3537/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003538{
3539 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003540}
3541#endif /* HAVE_FDATASYNC */
3542
3543
Fredrik Lundh10723342000-07-10 16:38:09 +00003544#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003545/*[clinic input]
3546os.chown
3547
3548 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003549 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003550
3551 uid: uid_t
3552
3553 gid: gid_t
3554
3555 *
3556
3557 dir_fd : dir_fd(requires='fchownat') = None
3558 If not None, it should be a file descriptor open to a directory,
3559 and path should be relative; path will then be relative to that
3560 directory.
3561
3562 follow_symlinks: bool = True
3563 If False, and the last element of the path is a symbolic link,
3564 stat will examine the symbolic link itself instead of the file
3565 the link points to.
3566
3567Change the owner and group id of path to the numeric uid and gid.\
3568
3569path may always be specified as a string.
3570On some platforms, path may also be specified as an open file descriptor.
3571 If this functionality is unavailable, using it raises an exception.
3572If dir_fd is not None, it should be a file descriptor open to a directory,
3573 and path should be relative; path will then be relative to that directory.
3574If follow_symlinks is False, and the last element of the path is a symbolic
3575 link, chown will modify the symbolic link itself instead of the file the
3576 link points to.
3577It is an error to use dir_fd or follow_symlinks when specifying path as
3578 an open file descriptor.
3579dir_fd and follow_symlinks may not be implemented on your platform.
3580 If they are unavailable, using them will raise a NotImplementedError.
3581
3582[clinic start generated code]*/
3583
Larry Hastings2f936352014-08-05 14:04:04 +10003584static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003585os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003586 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003587/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003588{
3589 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003590
Ronald Oussoren41761932020-11-08 10:05:27 +01003591#if defined(HAVE_FCHOWNAT)
3592 int fchownat_unsupported = 0;
3593#endif
3594
Larry Hastings9cf065c2012-06-22 16:30:09 -07003595#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3596 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003597 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003598#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003599 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3600 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3601 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003602
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003603 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
3604 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3605 return NULL;
3606 }
3607
Victor Stinner8c62be82010-05-06 00:08:46 +00003608 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003609#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003610 if (path->fd != -1)
3611 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003612 else
3613#endif
3614#ifdef HAVE_LCHOWN
3615 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003616 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003617 else
3618#endif
3619#ifdef HAVE_FCHOWNAT
Ronald Oussoren41761932020-11-08 10:05:27 +01003620 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
3621 if (HAVE_FCHOWNAT_RUNTIME) {
Larry Hastings2f936352014-08-05 14:04:04 +10003622 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003623 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
Ronald Oussoren41761932020-11-08 10:05:27 +01003624 } else {
3625 fchownat_unsupported = 1;
3626 }
3627 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07003628#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003629 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003630 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003631
Ronald Oussoren41761932020-11-08 10:05:27 +01003632#ifdef HAVE_FCHOWNAT
3633 if (fchownat_unsupported) {
3634 /* This would be incorrect if the current platform
3635 * doesn't support lchown.
3636 */
3637 argument_unavailable_error(NULL, "dir_fd");
3638 return NULL;
3639 }
3640#endif
3641
Larry Hastings2f936352014-08-05 14:04:04 +10003642 if (result)
3643 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003644
Larry Hastings2f936352014-08-05 14:04:04 +10003645 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003646}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003647#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003648
Larry Hastings2f936352014-08-05 14:04:04 +10003649
Christian Heimes4e30a842007-11-30 22:12:06 +00003650#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003651/*[clinic input]
3652os.fchown
3653
3654 fd: int
3655 uid: uid_t
3656 gid: gid_t
3657
3658Change the owner and group id of the file specified by file descriptor.
3659
3660Equivalent to os.chown(fd, uid, gid).
3661
3662[clinic start generated code]*/
3663
Larry Hastings2f936352014-08-05 14:04:04 +10003664static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003665os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3666/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003667{
Victor Stinner8c62be82010-05-06 00:08:46 +00003668 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003669 int async_err = 0;
3670
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003671 if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
3672 return NULL;
3673 }
3674
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003675 do {
3676 Py_BEGIN_ALLOW_THREADS
3677 res = fchown(fd, uid, gid);
3678 Py_END_ALLOW_THREADS
3679 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3680 if (res != 0)
3681 return (!async_err) ? posix_error() : NULL;
3682
Victor Stinner8c62be82010-05-06 00:08:46 +00003683 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003684}
3685#endif /* HAVE_FCHOWN */
3686
Larry Hastings2f936352014-08-05 14:04:04 +10003687
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003688#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003689/*[clinic input]
3690os.lchown
3691
3692 path : path_t
3693 uid: uid_t
3694 gid: gid_t
3695
3696Change the owner and group id of path to the numeric uid and gid.
3697
3698This function will not follow symbolic links.
3699Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3700[clinic start generated code]*/
3701
Larry Hastings2f936352014-08-05 14:04:04 +10003702static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003703os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3704/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003705{
Victor Stinner8c62be82010-05-06 00:08:46 +00003706 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003707 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
3708 return NULL;
3709 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003710 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003711 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003712 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003713 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003714 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003715 }
Larry Hastings2f936352014-08-05 14:04:04 +10003716 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003717}
3718#endif /* HAVE_LCHOWN */
3719
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003720
Barry Warsaw53699e91996-12-10 23:23:01 +00003721static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003722posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003723{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003724#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003725 wchar_t wbuf[MAXPATHLEN];
3726 wchar_t *wbuf2 = wbuf;
3727 DWORD len;
3728
3729 Py_BEGIN_ALLOW_THREADS
3730 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3731 /* If the buffer is large enough, len does not include the
3732 terminating \0. If the buffer is too small, len includes
3733 the space needed for the terminator. */
3734 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003735 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003736 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003737 }
Victor Stinner689830e2019-06-26 17:31:12 +02003738 else {
3739 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003740 }
Victor Stinner689830e2019-06-26 17:31:12 +02003741 if (wbuf2) {
3742 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003743 }
Victor Stinner689830e2019-06-26 17:31:12 +02003744 }
3745 Py_END_ALLOW_THREADS
3746
3747 if (!wbuf2) {
3748 PyErr_NoMemory();
3749 return NULL;
3750 }
3751 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003752 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003753 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003754 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003755 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003756
Victor Stinner689830e2019-06-26 17:31:12 +02003757 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3758 if (wbuf2 != wbuf) {
3759 PyMem_RawFree(wbuf2);
3760 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003761
Victor Stinner689830e2019-06-26 17:31:12 +02003762 if (use_bytes) {
3763 if (resobj == NULL) {
3764 return NULL;
3765 }
3766 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3767 }
3768
3769 return resobj;
3770#else
3771 const size_t chunk = 1024;
3772
3773 char *buf = NULL;
3774 char *cwd = NULL;
3775 size_t buflen = 0;
3776
Victor Stinner8c62be82010-05-06 00:08:46 +00003777 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003778 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003779 char *newbuf;
3780 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3781 buflen += chunk;
3782 newbuf = PyMem_RawRealloc(buf, buflen);
3783 }
3784 else {
3785 newbuf = NULL;
3786 }
3787 if (newbuf == NULL) {
3788 PyMem_RawFree(buf);
3789 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003790 break;
3791 }
Victor Stinner689830e2019-06-26 17:31:12 +02003792 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003793
Victor Stinner4403d7d2015-04-25 00:16:10 +02003794 cwd = getcwd(buf, buflen);
3795 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003796 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003797
Victor Stinner689830e2019-06-26 17:31:12 +02003798 if (buf == NULL) {
3799 return PyErr_NoMemory();
3800 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003801 if (cwd == NULL) {
3802 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003803 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003804 }
3805
Victor Stinner689830e2019-06-26 17:31:12 +02003806 PyObject *obj;
3807 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003808 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003809 }
3810 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003811 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003812 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003813 PyMem_RawFree(buf);
3814
3815 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003816#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003817}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003818
Larry Hastings2f936352014-08-05 14:04:04 +10003819
3820/*[clinic input]
3821os.getcwd
3822
3823Return a unicode string representing the current working directory.
3824[clinic start generated code]*/
3825
Larry Hastings2f936352014-08-05 14:04:04 +10003826static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003827os_getcwd_impl(PyObject *module)
3828/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003829{
3830 return posix_getcwd(0);
3831}
3832
Larry Hastings2f936352014-08-05 14:04:04 +10003833
3834/*[clinic input]
3835os.getcwdb
3836
3837Return a bytes string representing the current working directory.
3838[clinic start generated code]*/
3839
Larry Hastings2f936352014-08-05 14:04:04 +10003840static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003841os_getcwdb_impl(PyObject *module)
3842/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003843{
3844 return posix_getcwd(1);
3845}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003846
Larry Hastings2f936352014-08-05 14:04:04 +10003847
Larry Hastings9cf065c2012-06-22 16:30:09 -07003848#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3849#define HAVE_LINK 1
3850#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003851
Guido van Rossumb6775db1994-08-01 11:34:53 +00003852#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003853/*[clinic input]
3854
3855os.link
3856
3857 src : path_t
3858 dst : path_t
3859 *
3860 src_dir_fd : dir_fd = None
3861 dst_dir_fd : dir_fd = None
3862 follow_symlinks: bool = True
3863
3864Create a hard link to a file.
3865
3866If either src_dir_fd or dst_dir_fd is not None, it should be a file
3867 descriptor open to a directory, and the respective path string (src or dst)
3868 should be relative; the path will then be relative to that directory.
3869If follow_symlinks is False, and the last element of src is a symbolic
3870 link, link will create a link to the symbolic link itself instead of the
3871 file the link points to.
3872src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3873 platform. If they are unavailable, using them will raise a
3874 NotImplementedError.
3875[clinic start generated code]*/
3876
Larry Hastings2f936352014-08-05 14:04:04 +10003877static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003878os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003879 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003880/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003881{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003882#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003883 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003884#else
3885 int result;
3886#endif
Ronald Oussoren41761932020-11-08 10:05:27 +01003887#if defined(HAVE_LINKAT)
3888 int linkat_unavailable = 0;
3889#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003890
Larry Hastings9cf065c2012-06-22 16:30:09 -07003891#ifndef HAVE_LINKAT
3892 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3893 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003894 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003895 }
3896#endif
3897
Steve Dowercc16be82016-09-08 10:35:16 -07003898#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003899 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003900 PyErr_SetString(PyExc_NotImplementedError,
3901 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003902 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003903 }
Steve Dowercc16be82016-09-08 10:35:16 -07003904#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003905
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003906 if (PySys_Audit("os.link", "OOii", src->object, dst->object,
3907 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
3908 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
3909 return NULL;
3910 }
3911
Brian Curtin1b9df392010-11-24 20:24:31 +00003912#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003913 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003914 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003915 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003916
Larry Hastings2f936352014-08-05 14:04:04 +10003917 if (!result)
3918 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003919#else
3920 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003921#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003922 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3923 (dst_dir_fd != DEFAULT_DIR_FD) ||
Ronald Oussoren41761932020-11-08 10:05:27 +01003924 (!follow_symlinks)) {
3925
3926 if (HAVE_LINKAT_RUNTIME) {
3927
3928 result = linkat(src_dir_fd, src->narrow,
3929 dst_dir_fd, dst->narrow,
3930 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3931
3932 }
3933#ifdef __APPLE__
3934 else {
3935 if (src_dir_fd == DEFAULT_DIR_FD && dst_dir_fd == DEFAULT_DIR_FD) {
3936 /* See issue 41355: This matches the behaviour of !HAVE_LINKAT */
3937 result = link(src->narrow, dst->narrow);
3938 } else {
3939 linkat_unavailable = 1;
3940 }
3941 }
3942#endif
3943 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003944 else
Steve Dowercc16be82016-09-08 10:35:16 -07003945#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003946 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003947 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003948
Ronald Oussoren41761932020-11-08 10:05:27 +01003949#ifdef HAVE_LINKAT
3950 if (linkat_unavailable) {
3951 /* Either or both dir_fd arguments were specified */
3952 if (src_dir_fd != DEFAULT_DIR_FD) {
3953 argument_unavailable_error("link", "src_dir_fd");
3954 } else {
3955 argument_unavailable_error("link", "dst_dir_fd");
3956 }
3957 return NULL;
3958 }
3959#endif
3960
Larry Hastings2f936352014-08-05 14:04:04 +10003961 if (result)
3962 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003963#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003964
Larry Hastings2f936352014-08-05 14:04:04 +10003965 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003966}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003967#endif
3968
Brian Curtin1b9df392010-11-24 20:24:31 +00003969
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003970#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003971static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003972_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003973{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003974 PyObject *v;
3975 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3976 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003977 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003978 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003979 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003980 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003981
Steve Dowercc16be82016-09-08 10:35:16 -07003982 WIN32_FIND_DATAW wFileData;
3983 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003984
Steve Dowercc16be82016-09-08 10:35:16 -07003985 if (!path->wide) { /* Default arg: "." */
3986 po_wchars = L".";
3987 len = 1;
3988 } else {
3989 po_wchars = path->wide;
3990 len = wcslen(path->wide);
3991 }
3992 /* The +5 is so we can append "\\*.*\0" */
3993 wnamebuf = PyMem_New(wchar_t, len + 5);
3994 if (!wnamebuf) {
3995 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003996 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003997 }
Steve Dowercc16be82016-09-08 10:35:16 -07003998 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003999 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07004000 wchar_t wch = wnamebuf[len-1];
4001 if (wch != SEP && wch != ALTSEP && wch != L':')
4002 wnamebuf[len++] = SEP;
4003 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00004004 }
Steve Dowercc16be82016-09-08 10:35:16 -07004005 if ((list = PyList_New(0)) == NULL) {
4006 goto exit;
4007 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00004008 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004009 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00004010 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00004011 if (hFindFile == INVALID_HANDLE_VALUE) {
4012 int error = GetLastError();
4013 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004014 goto exit;
4015 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004016 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004017 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004018 }
4019 do {
4020 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07004021 if (wcscmp(wFileData.cFileName, L".") != 0 &&
4022 wcscmp(wFileData.cFileName, L"..") != 0) {
4023 v = PyUnicode_FromWideChar(wFileData.cFileName,
4024 wcslen(wFileData.cFileName));
4025 if (path->narrow && v) {
4026 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
4027 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004028 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004029 Py_DECREF(list);
4030 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004031 break;
4032 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004033 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004034 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004035 Py_DECREF(list);
4036 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00004037 break;
4038 }
4039 Py_DECREF(v);
4040 }
4041 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004042 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00004043 Py_END_ALLOW_THREADS
4044 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
4045 it got to the end of the directory. */
4046 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004047 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004048 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004049 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004050 }
4051 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004052
Larry Hastings9cf065c2012-06-22 16:30:09 -07004053exit:
4054 if (hFindFile != INVALID_HANDLE_VALUE) {
4055 if (FindClose(hFindFile) == FALSE) {
4056 if (list != NULL) {
4057 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004058 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004059 }
4060 }
Victor Stinner8c62be82010-05-06 00:08:46 +00004061 }
Victor Stinnerb6404912013-07-07 16:21:41 +02004062 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00004063
Larry Hastings9cf065c2012-06-22 16:30:09 -07004064 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004065} /* end of _listdir_windows_no_opendir */
4066
4067#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
4068
4069static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07004070_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004071{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004072 PyObject *v;
4073 DIR *dirp = NULL;
4074 struct dirent *ep;
4075 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004076#ifdef HAVE_FDOPENDIR
4077 int fd = -1;
4078#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004079
Victor Stinner8c62be82010-05-06 00:08:46 +00004080 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004081#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07004082 if (path->fd != -1) {
Ronald Oussoren41761932020-11-08 10:05:27 +01004083 if (HAVE_FDOPENDIR_RUNTIME) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004084 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02004085 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01004086 if (fd == -1)
4087 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004088
Larry Hastingsfdaea062012-06-25 04:42:23 -07004089 return_str = 1;
4090
Larry Hastings9cf065c2012-06-22 16:30:09 -07004091 Py_BEGIN_ALLOW_THREADS
4092 dirp = fdopendir(fd);
4093 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01004094 } else {
4095 PyErr_SetString(PyExc_TypeError,
4096 "listdir: path should be string, bytes, os.PathLike or None, not int");
4097 return NULL;
4098 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004099 }
4100 else
4101#endif
4102 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004103 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07004104 if (path->narrow) {
4105 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03004106 /* only return bytes if they specified a bytes-like object */
4107 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07004108 }
4109 else {
4110 name = ".";
4111 return_str = 1;
4112 }
4113
Larry Hastings9cf065c2012-06-22 16:30:09 -07004114 Py_BEGIN_ALLOW_THREADS
4115 dirp = opendir(name);
4116 Py_END_ALLOW_THREADS
4117 }
4118
4119 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07004120 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004121#ifdef HAVE_FDOPENDIR
4122 if (fd != -1) {
4123 Py_BEGIN_ALLOW_THREADS
4124 close(fd);
4125 Py_END_ALLOW_THREADS
4126 }
4127#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004128 goto exit;
4129 }
4130 if ((list = PyList_New(0)) == NULL) {
4131 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004132 }
4133 for (;;) {
4134 errno = 0;
4135 Py_BEGIN_ALLOW_THREADS
4136 ep = readdir(dirp);
4137 Py_END_ALLOW_THREADS
4138 if (ep == NULL) {
4139 if (errno == 0) {
4140 break;
4141 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004142 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07004143 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004144 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00004145 }
4146 }
4147 if (ep->d_name[0] == '.' &&
4148 (NAMLEN(ep) == 1 ||
4149 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
4150 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07004151 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00004152 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
4153 else
4154 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00004155 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004156 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00004157 break;
4158 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004159 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00004160 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004161 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00004162 break;
4163 }
4164 Py_DECREF(v);
4165 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00004166
Larry Hastings9cf065c2012-06-22 16:30:09 -07004167exit:
4168 if (dirp != NULL) {
4169 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004170#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07004171 if (fd > -1)
4172 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07004173#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004174 closedir(dirp);
4175 Py_END_ALLOW_THREADS
4176 }
4177
Larry Hastings9cf065c2012-06-22 16:30:09 -07004178 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07004179} /* end of _posix_listdir */
4180#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00004181
Larry Hastings2f936352014-08-05 14:04:04 +10004182
4183/*[clinic input]
4184os.listdir
4185
4186 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
4187
4188Return a list containing the names of the files in the directory.
4189
BNMetricsb9427072018-11-02 15:20:19 +00004190path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10004191 the filenames returned will also be bytes; in all other circumstances
4192 the filenames returned will be str.
4193If path is None, uses the path='.'.
4194On some platforms, path may also be specified as an open file descriptor;\
4195 the file descriptor must refer to a directory.
4196 If this functionality is unavailable, using it raises NotImplementedError.
4197
4198The list is in arbitrary order. It does not include the special
4199entries '.' and '..' even if they are present in the directory.
4200
4201
4202[clinic start generated code]*/
4203
Larry Hastings2f936352014-08-05 14:04:04 +10004204static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004205os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00004206/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004207{
Steve Dower60419a72019-06-24 08:42:54 -07004208 if (PySys_Audit("os.listdir", "O",
4209 path->object ? path->object : Py_None) < 0) {
4210 return NULL;
4211 }
Larry Hastings2f936352014-08-05 14:04:04 +10004212#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
4213 return _listdir_windows_no_opendir(path, NULL);
4214#else
4215 return _posix_listdir(path, NULL);
4216#endif
4217}
4218
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004219#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00004220/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03004221/*[clinic input]
4222os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02004223
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03004224 path: path_t
4225 /
4226
4227[clinic start generated code]*/
4228
4229static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004230os__getfullpathname_impl(PyObject *module, path_t *path)
4231/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03004232{
Victor Stinner3939c322019-06-25 15:02:43 +02004233 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02004234
Victor Stinner3939c322019-06-25 15:02:43 +02004235 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
4236 if (_Py_abspath(path->wide, &abspath) < 0) {
4237 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00004238 }
Victor Stinner3939c322019-06-25 15:02:43 +02004239 if (abspath == NULL) {
4240 return PyErr_NoMemory();
4241 }
4242
4243 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
4244 PyMem_RawFree(abspath);
4245 if (str == NULL) {
4246 return NULL;
4247 }
4248 if (path->narrow) {
4249 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
4250 }
4251 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10004252}
Brian Curtind40e6f72010-07-08 21:39:08 +00004253
Brian Curtind25aef52011-06-13 15:16:04 -05004254
Larry Hastings2f936352014-08-05 14:04:04 +10004255/*[clinic input]
4256os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00004257
Steve Dower23ad6d02018-02-22 10:39:10 -08004258 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004259 /
4260
4261A helper function for samepath on windows.
4262[clinic start generated code]*/
4263
Larry Hastings2f936352014-08-05 14:04:04 +10004264static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004265os__getfinalpathname_impl(PyObject *module, path_t *path)
4266/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00004267{
4268 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004269 wchar_t buf[MAXPATHLEN], *target_path = buf;
4270 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00004271 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10004272 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004273
Steve Dower23ad6d02018-02-22 10:39:10 -08004274 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004275 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08004276 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00004277 0, /* desired access */
4278 0, /* share mode */
4279 NULL, /* security attributes */
4280 OPEN_EXISTING,
4281 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
4282 FILE_FLAG_BACKUP_SEMANTICS,
4283 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004284 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004285
Steve Dower23ad6d02018-02-22 10:39:10 -08004286 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004287 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08004288 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004289
4290 /* We have a good handle to the target, use it to determine the
4291 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004292 while (1) {
4293 Py_BEGIN_ALLOW_THREADS
4294 result_length = GetFinalPathNameByHandleW(hFile, target_path,
4295 buf_size, VOLUME_NAME_DOS);
4296 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004297
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004298 if (!result_length) {
4299 result = win32_error_object("GetFinalPathNameByHandleW",
4300 path->object);
4301 goto cleanup;
4302 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004303
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004304 if (result_length < buf_size) {
4305 break;
4306 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004307
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004308 wchar_t *tmp;
4309 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
4310 result_length * sizeof(*tmp));
4311 if (!tmp) {
4312 result = PyErr_NoMemory();
4313 goto cleanup;
4314 }
4315
4316 buf_size = result_length;
4317 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08004318 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004319
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004320 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004321 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004322 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004323 }
Steve Dower23ad6d02018-02-22 10:39:10 -08004324
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004325cleanup:
4326 if (target_path != buf) {
4327 PyMem_Free(target_path);
4328 }
4329 CloseHandle(hFile);
4330 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10004331}
Brian Curtin62857742010-09-06 17:07:27 +00004332
Tim Golden6b528062013-08-01 12:44:00 +01004333
Larry Hastings2f936352014-08-05 14:04:04 +10004334/*[clinic input]
4335os._getvolumepathname
4336
Steve Dower23ad6d02018-02-22 10:39:10 -08004337 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004338
4339A helper function for ismount on Win32.
4340[clinic start generated code]*/
4341
Larry Hastings2f936352014-08-05 14:04:04 +10004342static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004343os__getvolumepathname_impl(PyObject *module, path_t *path)
4344/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004345{
4346 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004347 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01004348 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01004349 BOOL ret;
4350
Tim Golden6b528062013-08-01 12:44:00 +01004351 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08004352 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01004353
Victor Stinner850a18e2017-10-24 16:53:32 -07004354 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01004355 PyErr_SetString(PyExc_OverflowError, "path too long");
4356 return NULL;
4357 }
4358
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004359 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004360 if (mountpath == NULL)
4361 return PyErr_NoMemory();
4362
4363 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08004364 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004365 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004366 Py_END_ALLOW_THREADS
4367
4368 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004369 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004370 goto exit;
4371 }
4372 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004373 if (path->narrow)
4374 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004375
4376exit:
4377 PyMem_Free(mountpath);
4378 return result;
4379}
Tim Golden6b528062013-08-01 12:44:00 +01004380
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004381#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004382
Larry Hastings2f936352014-08-05 14:04:04 +10004383
4384/*[clinic input]
4385os.mkdir
4386
4387 path : path_t
4388
4389 mode: int = 0o777
4390
4391 *
4392
4393 dir_fd : dir_fd(requires='mkdirat') = None
4394
4395# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4396
4397Create a directory.
4398
4399If dir_fd is not None, it should be a file descriptor open to a directory,
4400 and path should be relative; path will then be relative to that directory.
4401dir_fd may not be implemented on your platform.
4402 If it is unavailable, using it will raise a NotImplementedError.
4403
4404The mode argument is ignored on Windows.
4405[clinic start generated code]*/
4406
Larry Hastings2f936352014-08-05 14:04:04 +10004407static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004408os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4409/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004410{
4411 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01004412#ifdef HAVE_MKDIRAT
4413 int mkdirat_unavailable = 0;
4414#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004415
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004416 if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
4417 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4418 return NULL;
4419 }
4420
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004421#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004422 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004423 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004424 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004425
Larry Hastings2f936352014-08-05 14:04:04 +10004426 if (!result)
4427 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004428#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004429 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004430#if HAVE_MKDIRAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004431 if (dir_fd != DEFAULT_DIR_FD) {
4432 if (HAVE_MKDIRAT_RUNTIME) {
Larry Hastings2f936352014-08-05 14:04:04 +10004433 result = mkdirat(dir_fd, path->narrow, mode);
Ronald Oussoren41761932020-11-08 10:05:27 +01004434
4435 } else {
4436 mkdirat_unavailable = 1;
4437 }
4438 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004439#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004440#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004441 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004442#else
Larry Hastings2f936352014-08-05 14:04:04 +10004443 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004444#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004445 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01004446
4447#if HAVE_MKDIRAT
4448 if (mkdirat_unavailable) {
4449 argument_unavailable_error(NULL, "dir_fd");
4450 return NULL;
4451 }
4452#endif
4453
Larry Hastings2f936352014-08-05 14:04:04 +10004454 if (result < 0)
4455 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004456#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004457 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004458}
4459
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004460
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004461/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4462#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004463#include <sys/resource.h>
4464#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004465
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004466
4467#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004468/*[clinic input]
4469os.nice
4470
4471 increment: int
4472 /
4473
4474Add increment to the priority of process and return the new priority.
4475[clinic start generated code]*/
4476
Larry Hastings2f936352014-08-05 14:04:04 +10004477static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004478os_nice_impl(PyObject *module, int increment)
4479/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004480{
4481 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004482
Victor Stinner8c62be82010-05-06 00:08:46 +00004483 /* There are two flavours of 'nice': one that returns the new
4484 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004485 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004486 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004487
Victor Stinner8c62be82010-05-06 00:08:46 +00004488 If we are of the nice family that returns the new priority, we
4489 need to clear errno before the call, and check if errno is filled
4490 before calling posix_error() on a returnvalue of -1, because the
4491 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004492
Victor Stinner8c62be82010-05-06 00:08:46 +00004493 errno = 0;
4494 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004495#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004496 if (value == 0)
4497 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004498#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004499 if (value == -1 && errno != 0)
4500 /* either nice() or getpriority() returned an error */
4501 return posix_error();
4502 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004503}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004504#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004505
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004506
4507#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004508/*[clinic input]
4509os.getpriority
4510
4511 which: int
4512 who: int
4513
4514Return program scheduling priority.
4515[clinic start generated code]*/
4516
Larry Hastings2f936352014-08-05 14:04:04 +10004517static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004518os_getpriority_impl(PyObject *module, int which, int who)
4519/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004520{
4521 int retval;
4522
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004523 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004524 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004525 if (errno != 0)
4526 return posix_error();
4527 return PyLong_FromLong((long)retval);
4528}
4529#endif /* HAVE_GETPRIORITY */
4530
4531
4532#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004533/*[clinic input]
4534os.setpriority
4535
4536 which: int
4537 who: int
4538 priority: int
4539
4540Set program scheduling priority.
4541[clinic start generated code]*/
4542
Larry Hastings2f936352014-08-05 14:04:04 +10004543static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004544os_setpriority_impl(PyObject *module, int which, int who, int priority)
4545/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004546{
4547 int retval;
4548
4549 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004550 if (retval == -1)
4551 return posix_error();
4552 Py_RETURN_NONE;
4553}
4554#endif /* HAVE_SETPRIORITY */
4555
4556
Barry Warsaw53699e91996-12-10 23:23:01 +00004557static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004558internal_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 +00004559{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004560 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004561 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004562
Ronald Oussoren41761932020-11-08 10:05:27 +01004563#ifdef HAVE_RENAMEAT
4564 int renameat_unavailable = 0;
4565#endif
4566
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004567#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004568 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004569 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004570#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004571 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004572#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004573
Larry Hastings9cf065c2012-06-22 16:30:09 -07004574 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4575 (dst_dir_fd != DEFAULT_DIR_FD);
4576#ifndef HAVE_RENAMEAT
4577 if (dir_fd_specified) {
4578 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004579 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004580 }
4581#endif
4582
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004583 if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
4584 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
4585 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
4586 return NULL;
4587 }
4588
Larry Hastings9cf065c2012-06-22 16:30:09 -07004589#ifdef MS_WINDOWS
4590 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004591 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004592 Py_END_ALLOW_THREADS
4593
Larry Hastings2f936352014-08-05 14:04:04 +10004594 if (!result)
4595 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004596
4597#else
Steve Dowercc16be82016-09-08 10:35:16 -07004598 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4599 PyErr_Format(PyExc_ValueError,
4600 "%s: src and dst must be the same type", function_name);
4601 return NULL;
4602 }
4603
Larry Hastings9cf065c2012-06-22 16:30:09 -07004604 Py_BEGIN_ALLOW_THREADS
4605#ifdef HAVE_RENAMEAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004606 if (dir_fd_specified) {
4607 if (HAVE_RENAMEAT_RUNTIME) {
4608 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
4609 } else {
4610 renameat_unavailable = 1;
4611 }
4612 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004613#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004614 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004615 Py_END_ALLOW_THREADS
4616
Ronald Oussoren41761932020-11-08 10:05:27 +01004617
4618#ifdef HAVE_RENAMEAT
4619 if (renameat_unavailable) {
4620 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
4621 return NULL;
4622 }
4623#endif
4624
Larry Hastings2f936352014-08-05 14:04:04 +10004625 if (result)
4626 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004627#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004628 Py_RETURN_NONE;
4629}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004630
Larry Hastings2f936352014-08-05 14:04:04 +10004631
4632/*[clinic input]
4633os.rename
4634
4635 src : path_t
4636 dst : path_t
4637 *
4638 src_dir_fd : dir_fd = None
4639 dst_dir_fd : dir_fd = None
4640
4641Rename a file or directory.
4642
4643If either src_dir_fd or dst_dir_fd is not None, it should be a file
4644 descriptor open to a directory, and the respective path string (src or dst)
4645 should be relative; the path will then be relative to that directory.
4646src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4647 If they are unavailable, using them will raise a NotImplementedError.
4648[clinic start generated code]*/
4649
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004650static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004651os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004652 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004653/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004654{
Larry Hastings2f936352014-08-05 14:04:04 +10004655 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004656}
4657
Larry Hastings2f936352014-08-05 14:04:04 +10004658
4659/*[clinic input]
4660os.replace = os.rename
4661
4662Rename a file or directory, overwriting the destination.
4663
4664If either src_dir_fd or dst_dir_fd is not None, it should be a file
4665 descriptor open to a directory, and the respective path string (src or dst)
4666 should be relative; the path will then be relative to that directory.
4667src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004668 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004669[clinic start generated code]*/
4670
Larry Hastings2f936352014-08-05 14:04:04 +10004671static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004672os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4673 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004674/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004675{
4676 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4677}
4678
4679
4680/*[clinic input]
4681os.rmdir
4682
4683 path: path_t
4684 *
4685 dir_fd: dir_fd(requires='unlinkat') = None
4686
4687Remove a directory.
4688
4689If dir_fd is not None, it should be a file descriptor open to a directory,
4690 and path should be relative; path will then be relative to that directory.
4691dir_fd may not be implemented on your platform.
4692 If it is unavailable, using it will raise a NotImplementedError.
4693[clinic start generated code]*/
4694
Larry Hastings2f936352014-08-05 14:04:04 +10004695static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004696os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4697/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004698{
4699 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01004700#ifdef HAVE_UNLINKAT
4701 int unlinkat_unavailable = 0;
4702#endif
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004703
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004704 if (PySys_Audit("os.rmdir", "Oi", path->object,
4705 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4706 return NULL;
4707 }
4708
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004709 Py_BEGIN_ALLOW_THREADS
4710#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004711 /* Windows, success=1, UNIX, success=0 */
4712 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004713#else
4714#ifdef HAVE_UNLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004715 if (dir_fd != DEFAULT_DIR_FD) {
4716 if (HAVE_UNLINKAT_RUNTIME) {
Larry Hastings2f936352014-08-05 14:04:04 +10004717 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Ronald Oussoren41761932020-11-08 10:05:27 +01004718 } else {
4719 unlinkat_unavailable = 1;
4720 result = -1;
4721 }
4722 } else
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004723#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004724 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004725#endif
4726 Py_END_ALLOW_THREADS
4727
Ronald Oussoren41761932020-11-08 10:05:27 +01004728#ifdef HAVE_UNLINKAT
4729 if (unlinkat_unavailable) {
4730 argument_unavailable_error("rmdir", "dir_fd");
4731 return NULL;
4732 }
4733#endif
4734
Larry Hastings2f936352014-08-05 14:04:04 +10004735 if (result)
4736 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004737
Larry Hastings2f936352014-08-05 14:04:04 +10004738 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004739}
4740
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004741
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004742#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004743#ifdef MS_WINDOWS
4744/*[clinic input]
4745os.system -> long
4746
4747 command: Py_UNICODE
4748
4749Execute the command in a subshell.
4750[clinic start generated code]*/
4751
Larry Hastings2f936352014-08-05 14:04:04 +10004752static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004753os_system_impl(PyObject *module, const Py_UNICODE *command)
4754/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004755{
4756 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004757
Steve Dowerfbe3c762019-10-18 00:52:15 -07004758 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004759 return -1;
4760 }
4761
Victor Stinner8c62be82010-05-06 00:08:46 +00004762 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004763 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004764 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004765 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004766 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004767 return result;
4768}
4769#else /* MS_WINDOWS */
4770/*[clinic input]
4771os.system -> long
4772
4773 command: FSConverter
4774
4775Execute the command in a subshell.
4776[clinic start generated code]*/
4777
Larry Hastings2f936352014-08-05 14:04:04 +10004778static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004779os_system_impl(PyObject *module, PyObject *command)
4780/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004781{
4782 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004783 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004784
Steve Dowerfbe3c762019-10-18 00:52:15 -07004785 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004786 return -1;
4787 }
4788
Larry Hastings2f936352014-08-05 14:04:04 +10004789 Py_BEGIN_ALLOW_THREADS
4790 result = system(bytes);
4791 Py_END_ALLOW_THREADS
4792 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004793}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004794#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004795#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004796
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004797
Larry Hastings2f936352014-08-05 14:04:04 +10004798/*[clinic input]
4799os.umask
4800
4801 mask: int
4802 /
4803
4804Set the current numeric umask and return the previous umask.
4805[clinic start generated code]*/
4806
Larry Hastings2f936352014-08-05 14:04:04 +10004807static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004808os_umask_impl(PyObject *module, int mask)
4809/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004810{
4811 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004812 if (i < 0)
4813 return posix_error();
4814 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004815}
4816
Brian Curtind40e6f72010-07-08 21:39:08 +00004817#ifdef MS_WINDOWS
4818
4819/* override the default DeleteFileW behavior so that directory
4820symlinks can be removed with this function, the same as with
4821Unix symlinks */
4822BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4823{
4824 WIN32_FILE_ATTRIBUTE_DATA info;
4825 WIN32_FIND_DATAW find_data;
4826 HANDLE find_data_handle;
4827 int is_directory = 0;
4828 int is_link = 0;
4829
4830 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4831 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004832
Brian Curtind40e6f72010-07-08 21:39:08 +00004833 /* Get WIN32_FIND_DATA structure for the path to determine if
4834 it is a symlink */
4835 if(is_directory &&
4836 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4837 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4838
4839 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004840 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4841 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4842 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4843 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004844 FindClose(find_data_handle);
4845 }
4846 }
4847 }
4848
4849 if (is_directory && is_link)
4850 return RemoveDirectoryW(lpFileName);
4851
4852 return DeleteFileW(lpFileName);
4853}
4854#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004855
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004856
Larry Hastings2f936352014-08-05 14:04:04 +10004857/*[clinic input]
4858os.unlink
4859
4860 path: path_t
4861 *
4862 dir_fd: dir_fd(requires='unlinkat')=None
4863
4864Remove a file (same as remove()).
4865
4866If dir_fd is not None, it should be a file descriptor open to a directory,
4867 and path should be relative; path will then be relative to that directory.
4868dir_fd may not be implemented on your platform.
4869 If it is unavailable, using it will raise a NotImplementedError.
4870
4871[clinic start generated code]*/
4872
Larry Hastings2f936352014-08-05 14:04:04 +10004873static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004874os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4875/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004876{
4877 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01004878#ifdef HAVE_UNLINKAT
4879 int unlinkat_unavailable = 0;
4880#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004881
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004882 if (PySys_Audit("os.remove", "Oi", path->object,
4883 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4884 return NULL;
4885 }
4886
Larry Hastings9cf065c2012-06-22 16:30:09 -07004887 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004888 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004889#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004890 /* Windows, success=1, UNIX, success=0 */
4891 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004892#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004893#ifdef HAVE_UNLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +01004894 if (dir_fd != DEFAULT_DIR_FD) {
4895 if (HAVE_UNLINKAT_RUNTIME) {
4896
Larry Hastings2f936352014-08-05 14:04:04 +10004897 result = unlinkat(dir_fd, path->narrow, 0);
Ronald Oussoren41761932020-11-08 10:05:27 +01004898 } else {
4899 unlinkat_unavailable = 1;
4900 }
4901 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004902#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004903 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004904#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004905 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004906 Py_END_ALLOW_THREADS
4907
Ronald Oussoren41761932020-11-08 10:05:27 +01004908#ifdef HAVE_UNLINKAT
4909 if (unlinkat_unavailable) {
4910 argument_unavailable_error(NULL, "dir_fd");
4911 return NULL;
4912 }
4913#endif
4914
Larry Hastings2f936352014-08-05 14:04:04 +10004915 if (result)
4916 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004917
Larry Hastings2f936352014-08-05 14:04:04 +10004918 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004919}
4920
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004921
Larry Hastings2f936352014-08-05 14:04:04 +10004922/*[clinic input]
4923os.remove = os.unlink
4924
4925Remove a file (same as unlink()).
4926
4927If dir_fd is not None, it should be a file descriptor open to a directory,
4928 and path should be relative; path will then be relative to that directory.
4929dir_fd may not be implemented on your platform.
4930 If it is unavailable, using it will raise a NotImplementedError.
4931[clinic start generated code]*/
4932
Larry Hastings2f936352014-08-05 14:04:04 +10004933static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004934os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4935/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004936{
4937 return os_unlink_impl(module, path, dir_fd);
4938}
4939
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004940
Larry Hastings605a62d2012-06-24 04:33:36 -07004941static PyStructSequence_Field uname_result_fields[] = {
4942 {"sysname", "operating system name"},
4943 {"nodename", "name of machine on network (implementation-defined)"},
4944 {"release", "operating system release"},
4945 {"version", "operating system version"},
4946 {"machine", "hardware identifier"},
4947 {NULL}
4948};
4949
4950PyDoc_STRVAR(uname_result__doc__,
4951"uname_result: Result from os.uname().\n\n\
4952This object may be accessed either as a tuple of\n\
4953 (sysname, nodename, release, version, machine),\n\
4954or via the attributes sysname, nodename, release, version, and machine.\n\
4955\n\
4956See os.uname for more information.");
4957
4958static PyStructSequence_Desc uname_result_desc = {
Eddie Elizondob3966632019-11-05 07:16:14 -08004959 MODNAME ".uname_result", /* name */
Larry Hastings605a62d2012-06-24 04:33:36 -07004960 uname_result__doc__, /* doc */
4961 uname_result_fields,
4962 5
4963};
4964
Larry Hastings605a62d2012-06-24 04:33:36 -07004965#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004966/*[clinic input]
4967os.uname
4968
4969Return an object identifying the current operating system.
4970
4971The object behaves like a named tuple with the following fields:
4972 (sysname, nodename, release, version, machine)
4973
4974[clinic start generated code]*/
4975
Larry Hastings2f936352014-08-05 14:04:04 +10004976static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004977os_uname_impl(PyObject *module)
4978/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004979{
Victor Stinner8c62be82010-05-06 00:08:46 +00004980 struct utsname u;
4981 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004982 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004983
Victor Stinner8c62be82010-05-06 00:08:46 +00004984 Py_BEGIN_ALLOW_THREADS
4985 res = uname(&u);
4986 Py_END_ALLOW_THREADS
4987 if (res < 0)
4988 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004989
Hai Shif707d942020-03-16 21:15:01 +08004990 PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08004991 value = PyStructSequence_New((PyTypeObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004992 if (value == NULL)
4993 return NULL;
4994
4995#define SET(i, field) \
4996 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004997 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004998 if (!o) { \
4999 Py_DECREF(value); \
5000 return NULL; \
5001 } \
5002 PyStructSequence_SET_ITEM(value, i, o); \
5003 } \
5004
5005 SET(0, u.sysname);
5006 SET(1, u.nodename);
5007 SET(2, u.release);
5008 SET(3, u.version);
5009 SET(4, u.machine);
5010
5011#undef SET
5012
5013 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00005014}
Guido van Rossumb6775db1994-08-01 11:34:53 +00005015#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00005016
Larry Hastings9e3e70b2011-09-08 19:29:07 -07005017
Larry Hastings9cf065c2012-06-22 16:30:09 -07005018
5019typedef struct {
5020 int now;
5021 time_t atime_s;
5022 long atime_ns;
5023 time_t mtime_s;
5024 long mtime_ns;
5025} utime_t;
5026
5027/*
Victor Stinner484df002014-10-09 13:52:31 +02005028 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07005029 * they also intentionally leak the declaration of a pointer named "time"
5030 */
5031#define UTIME_TO_TIMESPEC \
5032 struct timespec ts[2]; \
5033 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005034 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005035 time = NULL; \
5036 else { \
Victor Stinner484df002014-10-09 13:52:31 +02005037 ts[0].tv_sec = ut->atime_s; \
5038 ts[0].tv_nsec = ut->atime_ns; \
5039 ts[1].tv_sec = ut->mtime_s; \
5040 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005041 time = ts; \
5042 } \
5043
5044#define UTIME_TO_TIMEVAL \
5045 struct timeval tv[2]; \
5046 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005047 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005048 time = NULL; \
5049 else { \
Victor Stinner484df002014-10-09 13:52:31 +02005050 tv[0].tv_sec = ut->atime_s; \
5051 tv[0].tv_usec = ut->atime_ns / 1000; \
5052 tv[1].tv_sec = ut->mtime_s; \
5053 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005054 time = tv; \
5055 } \
5056
5057#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005058 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005059 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005060 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005061 time = NULL; \
5062 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005063 u.actime = ut->atime_s; \
5064 u.modtime = ut->mtime_s; \
5065 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005066 }
5067
5068#define UTIME_TO_TIME_T \
5069 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005070 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02005071 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005072 time = NULL; \
5073 else { \
Victor Stinner484df002014-10-09 13:52:31 +02005074 timet[0] = ut->atime_s; \
5075 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02005076 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07005077 } \
5078
5079
Victor Stinner528a9ab2015-09-03 21:30:26 +02005080#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005081
5082static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02005083utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005084{
Ronald Oussoren41761932020-11-08 10:05:27 +01005085#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
5086 if (HAVE_UTIMENSAT_RUNTIME) {
5087 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
5088 UTIME_TO_TIMESPEC;
5089 return utimensat(dir_fd, path, time, flags);
5090 } else {
5091 errno = ENOSYS;
5092 return -1;
5093 }
5094#elif defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005095 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
5096 UTIME_TO_TIMESPEC;
5097 return utimensat(dir_fd, path, time, flags);
5098#elif defined(HAVE_FUTIMESAT)
5099 UTIME_TO_TIMEVAL;
5100 /*
5101 * follow_symlinks will never be false here;
5102 * we only allow !follow_symlinks and dir_fd together
5103 * if we have utimensat()
5104 */
5105 assert(follow_symlinks);
5106 return futimesat(dir_fd, path, time);
5107#endif
5108}
5109
Larry Hastings2f936352014-08-05 14:04:04 +10005110 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
5111#else
5112 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07005113#endif
5114
Victor Stinner528a9ab2015-09-03 21:30:26 +02005115#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005116
5117static int
Victor Stinner484df002014-10-09 13:52:31 +02005118utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005119{
5120#ifdef HAVE_FUTIMENS
Ronald Oussoren41761932020-11-08 10:05:27 +01005121
5122 if (HAVE_FUTIMENS_RUNTIME) {
5123
Larry Hastings9cf065c2012-06-22 16:30:09 -07005124 UTIME_TO_TIMESPEC;
5125 return futimens(fd, time);
Ronald Oussoren41761932020-11-08 10:05:27 +01005126
5127 } else
5128#ifndef HAVE_FUTIMES
5129 {
5130 /* Not sure if this can happen */
5131 PyErr_SetString(
5132 PyExc_RuntimeError,
5133 "neither futimens nor futimes are supported"
5134 " on this system");
5135 return -1;
5136 }
5137#endif
5138
5139#endif
5140#ifdef HAVE_FUTIMES
5141 {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005142 UTIME_TO_TIMEVAL;
5143 return futimes(fd, time);
Ronald Oussoren41761932020-11-08 10:05:27 +01005144 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005145#endif
5146}
5147
Larry Hastings2f936352014-08-05 14:04:04 +10005148 #define PATH_UTIME_HAVE_FD 1
5149#else
5150 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07005151#endif
5152
Victor Stinner5ebae872015-09-22 01:29:33 +02005153#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
5154# define UTIME_HAVE_NOFOLLOW_SYMLINKS
5155#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07005156
Victor Stinner4552ced2015-09-21 22:37:15 +02005157#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005158
5159static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02005160utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005161{
5162#ifdef HAVE_UTIMENSAT
Ronald Oussoren41761932020-11-08 10:05:27 +01005163 if (HAVE_UTIMENSAT_RUNTIME) {
5164 UTIME_TO_TIMESPEC;
5165 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
5166 } else
5167#ifndef HAVE_LUTIMES
5168 {
5169 /* Not sure if this can happen */
5170 PyErr_SetString(
5171 PyExc_RuntimeError,
5172 "neither utimensat nor lutimes are supported"
5173 " on this system");
5174 return -1;
5175 }
5176#endif
5177#endif
5178
5179#ifdef HAVE_LUTIMES
5180 {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005181 UTIME_TO_TIMEVAL;
5182 return lutimes(path, time);
Ronald Oussoren41761932020-11-08 10:05:27 +01005183 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005184#endif
5185}
5186
5187#endif
5188
5189#ifndef MS_WINDOWS
5190
5191static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02005192utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005193{
Ronald Oussoren41761932020-11-08 10:05:27 +01005194#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
5195 if (HAVE_UTIMENSAT_RUNTIME) {
5196 UTIME_TO_TIMESPEC;
5197 return utimensat(DEFAULT_DIR_FD, path, time, 0);
5198 } else {
5199 UTIME_TO_TIMEVAL;
5200 return utimes(path, time);
5201 }
5202#elif defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005203 UTIME_TO_TIMESPEC;
5204 return utimensat(DEFAULT_DIR_FD, path, time, 0);
5205#elif defined(HAVE_UTIMES)
5206 UTIME_TO_TIMEVAL;
5207 return utimes(path, time);
5208#elif defined(HAVE_UTIME_H)
5209 UTIME_TO_UTIMBUF;
5210 return utime(path, time);
5211#else
5212 UTIME_TO_TIME_T;
5213 return utime(path, time);
5214#endif
5215}
5216
5217#endif
5218
Larry Hastings76ad59b2012-05-03 00:30:07 -07005219static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02005220split_py_long_to_s_and_ns(PyObject *module, PyObject *py_long, time_t *s, long *ns)
Larry Hastings76ad59b2012-05-03 00:30:07 -07005221{
5222 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04005223 PyObject *divmod;
Victor Stinner1c2fa782020-05-10 11:05:29 +02005224 divmod = PyNumber_Divmod(py_long, get_posix_state(module)->billion);
Larry Hastings76ad59b2012-05-03 00:30:07 -07005225 if (!divmod)
5226 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03005227 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
5228 PyErr_Format(PyExc_TypeError,
5229 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -08005230 _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
Oren Milman0bd1a2d2018-09-12 22:14:35 +03005231 goto exit;
5232 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07005233 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
5234 if ((*s == -1) && PyErr_Occurred())
5235 goto exit;
5236 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04005237 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07005238 goto exit;
5239
5240 result = 1;
5241exit:
5242 Py_XDECREF(divmod);
5243 return result;
5244}
5245
Larry Hastings2f936352014-08-05 14:04:04 +10005246
5247/*[clinic input]
5248os.utime
5249
5250 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005251 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10005252 *
5253 ns: object = NULL
5254 dir_fd: dir_fd(requires='futimensat') = None
5255 follow_symlinks: bool=True
5256
Martin Panter0ff89092015-09-09 01:56:53 +00005257# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10005258
5259Set the access and modified time of path.
5260
5261path may always be specified as a string.
5262On some platforms, path may also be specified as an open file descriptor.
5263 If this functionality is unavailable, using it raises an exception.
5264
5265If times is not None, it must be a tuple (atime, mtime);
5266 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00005267If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10005268 atime_ns and mtime_ns should be expressed as integer nanoseconds
5269 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00005270If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10005271Specifying tuples for both times and ns is an error.
5272
5273If dir_fd is not None, it should be a file descriptor open to a directory,
5274 and path should be relative; path will then be relative to that directory.
5275If follow_symlinks is False, and the last element of the path is a symbolic
5276 link, utime will modify the symbolic link itself instead of the file the
5277 link points to.
5278It is an error to use dir_fd or follow_symlinks when specifying path
5279 as an open file descriptor.
5280dir_fd and follow_symlinks may not be available on your platform.
5281 If they are unavailable, using them will raise a NotImplementedError.
5282
5283[clinic start generated code]*/
5284
Larry Hastings2f936352014-08-05 14:04:04 +10005285static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005286os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
5287 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005288/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005289{
Larry Hastings9cf065c2012-06-22 16:30:09 -07005290#ifdef MS_WINDOWS
5291 HANDLE hFile;
5292 FILETIME atime, mtime;
5293#else
5294 int result;
5295#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07005296
Larry Hastings2f936352014-08-05 14:04:04 +10005297 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005298
Christian Heimesb3c87242013-08-01 00:08:16 +02005299 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07005300
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005301 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005302 PyErr_SetString(PyExc_ValueError,
5303 "utime: you may specify either 'times'"
5304 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005305 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005306 }
5307
Serhiy Storchaka279f4462019-09-14 12:24:05 +03005308 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02005309 time_t a_sec, m_sec;
5310 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005311 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005312 PyErr_SetString(PyExc_TypeError,
5313 "utime: 'times' must be either"
5314 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005315 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005316 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005317 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04005318 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02005319 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04005320 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02005321 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005322 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005323 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02005324 utime.atime_s = a_sec;
5325 utime.atime_ns = a_nsec;
5326 utime.mtime_s = m_sec;
5327 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005328 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005329 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07005330 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07005331 PyErr_SetString(PyExc_TypeError,
5332 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005333 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005334 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005335 utime.now = 0;
Victor Stinner1c2fa782020-05-10 11:05:29 +02005336 if (!split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07005337 &utime.atime_s, &utime.atime_ns) ||
Victor Stinner1c2fa782020-05-10 11:05:29 +02005338 !split_py_long_to_s_and_ns(module, PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07005339 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005340 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005341 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07005342 }
5343 else {
5344 /* times and ns are both None/unspecified. use "now". */
5345 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005346 }
5347
Victor Stinner4552ced2015-09-21 22:37:15 +02005348#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005349 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005350 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07005351#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04005352
Larry Hastings2f936352014-08-05 14:04:04 +10005353 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
5354 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
5355 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005356 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07005357
Larry Hastings9cf065c2012-06-22 16:30:09 -07005358#if !defined(HAVE_UTIMENSAT)
5359 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02005360 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005361 "utime: cannot use dir_fd and follow_symlinks "
5362 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005363 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07005364 }
5365#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07005366
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005367 if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
5368 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
5369 return NULL;
5370 }
5371
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005372#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005373 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07005374 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
5375 NULL, OPEN_EXISTING,
5376 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005377 Py_END_ALLOW_THREADS
5378 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10005379 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005380 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005381 }
5382
Larry Hastings9cf065c2012-06-22 16:30:09 -07005383 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01005384 GetSystemTimeAsFileTime(&mtime);
5385 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00005386 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005387 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08005388 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
5389 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00005390 }
5391 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
5392 /* Avoid putting the file name into the error here,
5393 as that may confuse the user into believing that
5394 something is wrong with the file, when it also
5395 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01005396 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005397 CloseHandle(hFile);
5398 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005399 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005400 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005401#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005402 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00005403
Victor Stinner4552ced2015-09-21 22:37:15 +02005404#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005405 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10005406 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005407 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07005408#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07005409
Victor Stinner528a9ab2015-09-03 21:30:26 +02005410#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Ronald Oussoren41761932020-11-08 10:05:27 +01005411 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks)) {
Larry Hastings2f936352014-08-05 14:04:04 +10005412 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Ronald Oussoren41761932020-11-08 10:05:27 +01005413
5414 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07005415#endif
5416
Victor Stinner528a9ab2015-09-03 21:30:26 +02005417#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10005418 if (path->fd != -1)
5419 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005420 else
5421#endif
5422
Larry Hastings2f936352014-08-05 14:04:04 +10005423 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005424
5425 Py_END_ALLOW_THREADS
5426
Ronald Oussoren41761932020-11-08 10:05:27 +01005427#if defined(__APPLE__) && defined(HAVE_UTIMENSAT)
5428 /* See utime_dir_fd implementation */
5429 if (result == -1 && errno == ENOSYS) {
5430 argument_unavailable_error(NULL, "dir_fd");
5431 return NULL;
5432 }
5433#endif
5434
Larry Hastings9cf065c2012-06-22 16:30:09 -07005435 if (result < 0) {
5436 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005437 posix_error();
5438 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005439 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07005440
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005441#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005442
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005443 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005444}
5445
Guido van Rossum3b066191991-06-04 19:40:25 +00005446/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005447
Larry Hastings2f936352014-08-05 14:04:04 +10005448
5449/*[clinic input]
5450os._exit
5451
5452 status: int
5453
5454Exit to the system with specified status, without normal exit processing.
5455[clinic start generated code]*/
5456
Larry Hastings2f936352014-08-05 14:04:04 +10005457static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005458os__exit_impl(PyObject *module, int status)
5459/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005460{
5461 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00005462 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005463}
5464
Steve Dowercc16be82016-09-08 10:35:16 -07005465#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5466#define EXECV_CHAR wchar_t
5467#else
5468#define EXECV_CHAR char
5469#endif
5470
pxinwrf2d7ac72019-05-21 18:46:37 +08005471#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005472static void
Steve Dowercc16be82016-09-08 10:35:16 -07005473free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005474{
Victor Stinner8c62be82010-05-06 00:08:46 +00005475 Py_ssize_t i;
5476 for (i = 0; i < count; i++)
5477 PyMem_Free(array[i]);
5478 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005479}
Martin v. Löwis011e8422009-05-05 04:43:17 +00005480
Berker Peksag81816462016-09-15 20:19:47 +03005481static int
5482fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00005483{
Victor Stinner8c62be82010-05-06 00:08:46 +00005484 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03005485 PyObject *ub;
5486 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07005487#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03005488 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07005489 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005490 *out = PyUnicode_AsWideCharString(ub, &size);
5491 if (*out)
5492 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07005493#else
Berker Peksag81816462016-09-15 20:19:47 +03005494 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00005495 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005496 size = PyBytes_GET_SIZE(ub);
5497 *out = PyMem_Malloc(size + 1);
5498 if (*out) {
5499 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5500 result = 1;
5501 } else
Victor Stinner50abf222013-11-07 23:56:10 +01005502 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07005503#endif
Berker Peksag81816462016-09-15 20:19:47 +03005504 Py_DECREF(ub);
5505 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005506}
Martin v. Löwis114619e2002-10-07 06:44:21 +00005507#endif
5508
pxinwrf2d7ac72019-05-21 18:46:37 +08005509#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07005510static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00005511parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5512{
Victor Stinner8c62be82010-05-06 00:08:46 +00005513 Py_ssize_t i, pos, envc;
5514 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005515 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005516 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005517
Victor Stinner8c62be82010-05-06 00:08:46 +00005518 i = PyMapping_Size(env);
5519 if (i < 0)
5520 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005521 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005522 if (envlist == NULL) {
5523 PyErr_NoMemory();
5524 return NULL;
5525 }
5526 envc = 0;
5527 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005528 if (!keys)
5529 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005530 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005531 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005532 goto error;
5533 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5534 PyErr_Format(PyExc_TypeError,
5535 "env.keys() or env.values() is not a list");
5536 goto error;
5537 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005538
Victor Stinner8c62be82010-05-06 00:08:46 +00005539 for (pos = 0; pos < i; pos++) {
5540 key = PyList_GetItem(keys, pos);
5541 val = PyList_GetItem(vals, pos);
5542 if (!key || !val)
5543 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005544
Berker Peksag81816462016-09-15 20:19:47 +03005545#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5546 if (!PyUnicode_FSDecoder(key, &key2))
5547 goto error;
5548 if (!PyUnicode_FSDecoder(val, &val2)) {
5549 Py_DECREF(key2);
5550 goto error;
5551 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005552 /* Search from index 1 because on Windows starting '=' is allowed for
5553 defining hidden environment variables. */
5554 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5555 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5556 {
5557 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005558 Py_DECREF(key2);
5559 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005560 goto error;
5561 }
Berker Peksag81816462016-09-15 20:19:47 +03005562 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5563#else
5564 if (!PyUnicode_FSConverter(key, &key2))
5565 goto error;
5566 if (!PyUnicode_FSConverter(val, &val2)) {
5567 Py_DECREF(key2);
5568 goto error;
5569 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005570 if (PyBytes_GET_SIZE(key2) == 0 ||
5571 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5572 {
5573 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005574 Py_DECREF(key2);
5575 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005576 goto error;
5577 }
Berker Peksag81816462016-09-15 20:19:47 +03005578 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5579 PyBytes_AS_STRING(val2));
5580#endif
5581 Py_DECREF(key2);
5582 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005583 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005584 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005585
5586 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5587 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005588 goto error;
5589 }
Berker Peksag81816462016-09-15 20:19:47 +03005590
Steve Dowercc16be82016-09-08 10:35:16 -07005591 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005592 }
5593 Py_DECREF(vals);
5594 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005595
Victor Stinner8c62be82010-05-06 00:08:46 +00005596 envlist[envc] = 0;
5597 *envc_ptr = envc;
5598 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005599
5600error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005601 Py_XDECREF(keys);
5602 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005603 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005604 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005605}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005606
Steve Dowercc16be82016-09-08 10:35:16 -07005607static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005608parse_arglist(PyObject* argv, Py_ssize_t *argc)
5609{
5610 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005611 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005612 if (argvlist == NULL) {
5613 PyErr_NoMemory();
5614 return NULL;
5615 }
5616 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005617 PyObject* item = PySequence_ITEM(argv, i);
5618 if (item == NULL)
5619 goto fail;
5620 if (!fsconvert_strdup(item, &argvlist[i])) {
5621 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005622 goto fail;
5623 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005624 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005625 }
5626 argvlist[*argc] = NULL;
5627 return argvlist;
5628fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005629 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005630 free_string_array(argvlist, *argc);
5631 return NULL;
5632}
Steve Dowercc16be82016-09-08 10:35:16 -07005633
Ross Lagerwall7807c352011-03-17 20:20:30 +02005634#endif
5635
Larry Hastings2f936352014-08-05 14:04:04 +10005636
Ross Lagerwall7807c352011-03-17 20:20:30 +02005637#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005638/*[clinic input]
5639os.execv
5640
Steve Dowercc16be82016-09-08 10:35:16 -07005641 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005642 Path of executable file.
5643 argv: object
5644 Tuple or list of strings.
5645 /
5646
5647Execute an executable path with arguments, replacing current process.
5648[clinic start generated code]*/
5649
Larry Hastings2f936352014-08-05 14:04:04 +10005650static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005651os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5652/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005653{
Steve Dowercc16be82016-09-08 10:35:16 -07005654 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005655 Py_ssize_t argc;
5656
5657 /* execv has two arguments: (path, argv), where
5658 argv is a list or tuple of strings. */
5659
Ross Lagerwall7807c352011-03-17 20:20:30 +02005660 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5661 PyErr_SetString(PyExc_TypeError,
5662 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005663 return NULL;
5664 }
5665 argc = PySequence_Size(argv);
5666 if (argc < 1) {
5667 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005668 return NULL;
5669 }
5670
5671 argvlist = parse_arglist(argv, &argc);
5672 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005673 return NULL;
5674 }
Steve Dowerbce26262016-11-19 19:17:26 -08005675 if (!argvlist[0][0]) {
5676 PyErr_SetString(PyExc_ValueError,
5677 "execv() arg 2 first element cannot be empty");
5678 free_string_array(argvlist, argc);
5679 return NULL;
5680 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005681
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005682 if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005683 free_string_array(argvlist, argc);
5684 return NULL;
5685 }
5686
Steve Dowerbce26262016-11-19 19:17:26 -08005687 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005688#ifdef HAVE_WEXECV
5689 _wexecv(path->wide, argvlist);
5690#else
5691 execv(path->narrow, argvlist);
5692#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005693 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005694
5695 /* If we get here it's definitely an error */
5696
5697 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005698 return posix_error();
5699}
5700
Larry Hastings2f936352014-08-05 14:04:04 +10005701
5702/*[clinic input]
5703os.execve
5704
5705 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5706 Path of executable file.
5707 argv: object
5708 Tuple or list of strings.
5709 env: object
5710 Dictionary of strings mapping to strings.
5711
5712Execute an executable path with arguments, replacing current process.
5713[clinic start generated code]*/
5714
Larry Hastings2f936352014-08-05 14:04:04 +10005715static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005716os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5717/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005718{
Steve Dowercc16be82016-09-08 10:35:16 -07005719 EXECV_CHAR **argvlist = NULL;
5720 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005721 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005722
Victor Stinner8c62be82010-05-06 00:08:46 +00005723 /* execve has three arguments: (path, argv, env), where
5724 argv is a list or tuple of strings and env is a dictionary
5725 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005726
Ross Lagerwall7807c352011-03-17 20:20:30 +02005727 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005728 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005729 "execve: argv must be a tuple or list");
Saiyang Gou95f60012020-02-04 16:15:00 -08005730 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005731 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005732 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005733 if (argc < 1) {
5734 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5735 return NULL;
5736 }
5737
Victor Stinner8c62be82010-05-06 00:08:46 +00005738 if (!PyMapping_Check(env)) {
5739 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005740 "execve: environment must be a mapping object");
Saiyang Gou95f60012020-02-04 16:15:00 -08005741 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005742 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005743
Ross Lagerwall7807c352011-03-17 20:20:30 +02005744 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005745 if (argvlist == NULL) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005746 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005747 }
Steve Dowerbce26262016-11-19 19:17:26 -08005748 if (!argvlist[0][0]) {
5749 PyErr_SetString(PyExc_ValueError,
5750 "execve: argv first element cannot be empty");
Saiyang Gou95f60012020-02-04 16:15:00 -08005751 goto fail_0;
Steve Dowerbce26262016-11-19 19:17:26 -08005752 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005753
Victor Stinner8c62be82010-05-06 00:08:46 +00005754 envlist = parse_envlist(env, &envc);
5755 if (envlist == NULL)
Saiyang Gou95f60012020-02-04 16:15:00 -08005756 goto fail_0;
5757
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005758 if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005759 goto fail_1;
5760 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005761
Steve Dowerbce26262016-11-19 19:17:26 -08005762 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005763#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005764 if (path->fd > -1)
5765 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005766 else
5767#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005768#ifdef HAVE_WEXECV
5769 _wexecve(path->wide, argvlist, envlist);
5770#else
Larry Hastings2f936352014-08-05 14:04:04 +10005771 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005772#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005773 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005774
5775 /* If we get here it's definitely an error */
5776
Alexey Izbyshev83460312018-10-20 03:28:22 +03005777 posix_path_error(path);
Saiyang Gou95f60012020-02-04 16:15:00 -08005778 fail_1:
Steve Dowercc16be82016-09-08 10:35:16 -07005779 free_string_array(envlist, envc);
Saiyang Gou95f60012020-02-04 16:15:00 -08005780 fail_0:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005781 if (argvlist)
5782 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005783 return NULL;
5784}
Steve Dowercc16be82016-09-08 10:35:16 -07005785
Larry Hastings9cf065c2012-06-22 16:30:09 -07005786#endif /* HAVE_EXECV */
5787
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005788#ifdef HAVE_POSIX_SPAWN
5789
5790enum posix_spawn_file_actions_identifier {
5791 POSIX_SPAWN_OPEN,
5792 POSIX_SPAWN_CLOSE,
5793 POSIX_SPAWN_DUP2
5794};
5795
William Orr81574b82018-10-01 22:19:56 -07005796#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005797static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02005798convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005799#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005800
5801static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02005802parse_posix_spawn_flags(PyObject *module, const char *func_name, PyObject *setpgroup,
Victor Stinner325e4ba2019-02-01 15:47:24 +01005803 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005804 PyObject *setsigdef, PyObject *scheduler,
5805 posix_spawnattr_t *attrp)
5806{
5807 long all_flags = 0;
5808
5809 errno = posix_spawnattr_init(attrp);
5810 if (errno) {
5811 posix_error();
5812 return -1;
5813 }
5814
5815 if (setpgroup) {
5816 pid_t pgid = PyLong_AsPid(setpgroup);
5817 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5818 goto fail;
5819 }
5820 errno = posix_spawnattr_setpgroup(attrp, pgid);
5821 if (errno) {
5822 posix_error();
5823 goto fail;
5824 }
5825 all_flags |= POSIX_SPAWN_SETPGROUP;
5826 }
5827
5828 if (resetids) {
5829 all_flags |= POSIX_SPAWN_RESETIDS;
5830 }
5831
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005832 if (setsid) {
Ronald Oussoren41761932020-11-08 10:05:27 +01005833#ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
5834 if (HAVE_POSIX_SPAWN_SETSID_RUNTIME) {
5835#endif
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005836#ifdef POSIX_SPAWN_SETSID
5837 all_flags |= POSIX_SPAWN_SETSID;
5838#elif defined(POSIX_SPAWN_SETSID_NP)
5839 all_flags |= POSIX_SPAWN_SETSID_NP;
5840#else
5841 argument_unavailable_error(func_name, "setsid");
5842 return -1;
5843#endif
Ronald Oussoren41761932020-11-08 10:05:27 +01005844
5845#ifdef HAVE_POSIX_SPAWN_SETSID_RUNTIME
5846 } else {
5847 argument_unavailable_error(func_name, "setsid");
5848 return -1;
5849 }
5850#endif /* HAVE_POSIX_SPAWN_SETSID_RUNTIME */
5851
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005852 }
5853
Pablo Galindo254a4662018-09-07 16:44:24 +01005854 if (setsigmask) {
5855 sigset_t set;
5856 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5857 goto fail;
5858 }
5859 errno = posix_spawnattr_setsigmask(attrp, &set);
5860 if (errno) {
5861 posix_error();
5862 goto fail;
5863 }
5864 all_flags |= POSIX_SPAWN_SETSIGMASK;
5865 }
5866
5867 if (setsigdef) {
5868 sigset_t set;
5869 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5870 goto fail;
5871 }
5872 errno = posix_spawnattr_setsigdefault(attrp, &set);
5873 if (errno) {
5874 posix_error();
5875 goto fail;
5876 }
5877 all_flags |= POSIX_SPAWN_SETSIGDEF;
5878 }
5879
5880 if (scheduler) {
5881#ifdef POSIX_SPAWN_SETSCHEDULER
5882 PyObject *py_schedpolicy;
Victor Stinner1c2fa782020-05-10 11:05:29 +02005883 PyObject *schedparam_obj;
Pablo Galindo254a4662018-09-07 16:44:24 +01005884 struct sched_param schedparam;
5885
Victor Stinner1c2fa782020-05-10 11:05:29 +02005886 if (!PyArg_ParseTuple(scheduler, "OO"
Pablo Galindo254a4662018-09-07 16:44:24 +01005887 ";A scheduler tuple must have two elements",
Victor Stinner1c2fa782020-05-10 11:05:29 +02005888 &py_schedpolicy, &schedparam_obj)) {
5889 goto fail;
5890 }
5891 if (!convert_sched_param(module, schedparam_obj, &schedparam)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005892 goto fail;
5893 }
5894 if (py_schedpolicy != Py_None) {
5895 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5896
5897 if (schedpolicy == -1 && PyErr_Occurred()) {
5898 goto fail;
5899 }
5900 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5901 if (errno) {
5902 posix_error();
5903 goto fail;
5904 }
5905 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5906 }
5907 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5908 if (errno) {
5909 posix_error();
5910 goto fail;
5911 }
5912 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5913#else
5914 PyErr_SetString(PyExc_NotImplementedError,
5915 "The scheduler option is not supported in this system.");
5916 goto fail;
5917#endif
5918 }
5919
5920 errno = posix_spawnattr_setflags(attrp, all_flags);
5921 if (errno) {
5922 posix_error();
5923 goto fail;
5924 }
5925
5926 return 0;
5927
5928fail:
5929 (void)posix_spawnattr_destroy(attrp);
5930 return -1;
5931}
5932
5933static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005934parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005935 posix_spawn_file_actions_t *file_actionsp,
5936 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005937{
5938 PyObject *seq;
5939 PyObject *file_action = NULL;
5940 PyObject *tag_obj;
5941
5942 seq = PySequence_Fast(file_actions,
5943 "file_actions must be a sequence or None");
5944 if (seq == NULL) {
5945 return -1;
5946 }
5947
5948 errno = posix_spawn_file_actions_init(file_actionsp);
5949 if (errno) {
5950 posix_error();
5951 Py_DECREF(seq);
5952 return -1;
5953 }
5954
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005955 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005956 file_action = PySequence_Fast_GET_ITEM(seq, i);
5957 Py_INCREF(file_action);
5958 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5959 PyErr_SetString(PyExc_TypeError,
5960 "Each file_actions element must be a non-empty tuple");
5961 goto fail;
5962 }
5963 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5964 if (tag == -1 && PyErr_Occurred()) {
5965 goto fail;
5966 }
5967
5968 /* Populate the file_actions object */
5969 switch (tag) {
5970 case POSIX_SPAWN_OPEN: {
5971 int fd, oflag;
5972 PyObject *path;
5973 unsigned long mode;
5974 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5975 ";A open file_action tuple must have 5 elements",
5976 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5977 &oflag, &mode))
5978 {
5979 goto fail;
5980 }
Pablo Galindocb970732018-06-19 09:19:50 +01005981 if (PyList_Append(temp_buffer, path)) {
5982 Py_DECREF(path);
5983 goto fail;
5984 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005985 errno = posix_spawn_file_actions_addopen(file_actionsp,
5986 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005987 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005988 if (errno) {
5989 posix_error();
5990 goto fail;
5991 }
5992 break;
5993 }
5994 case POSIX_SPAWN_CLOSE: {
5995 int fd;
5996 if (!PyArg_ParseTuple(file_action, "Oi"
5997 ";A close file_action tuple must have 2 elements",
5998 &tag_obj, &fd))
5999 {
6000 goto fail;
6001 }
6002 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
6003 if (errno) {
6004 posix_error();
6005 goto fail;
6006 }
6007 break;
6008 }
6009 case POSIX_SPAWN_DUP2: {
6010 int fd1, fd2;
6011 if (!PyArg_ParseTuple(file_action, "Oii"
6012 ";A dup2 file_action tuple must have 3 elements",
6013 &tag_obj, &fd1, &fd2))
6014 {
6015 goto fail;
6016 }
6017 errno = posix_spawn_file_actions_adddup2(file_actionsp,
6018 fd1, fd2);
6019 if (errno) {
6020 posix_error();
6021 goto fail;
6022 }
6023 break;
6024 }
6025 default: {
6026 PyErr_SetString(PyExc_TypeError,
6027 "Unknown file_actions identifier");
6028 goto fail;
6029 }
6030 }
6031 Py_DECREF(file_action);
6032 }
Pablo Galindo254a4662018-09-07 16:44:24 +01006033
Serhiy Storchakaef347532018-05-01 16:45:04 +03006034 Py_DECREF(seq);
6035 return 0;
6036
6037fail:
6038 Py_DECREF(seq);
6039 Py_DECREF(file_action);
6040 (void)posix_spawn_file_actions_destroy(file_actionsp);
6041 return -1;
6042}
6043
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006044
6045static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006046py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
6047 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006048 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006049 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006050{
Victor Stinner325e4ba2019-02-01 15:47:24 +01006051 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006052 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006053 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03006054 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006055 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01006056 posix_spawnattr_t attr;
6057 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006058 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03006059 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01006060 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03006061 pid_t pid;
6062 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006063
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006064 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03006065 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006066 like posix.environ. */
6067
Serhiy Storchakaef347532018-05-01 16:45:04 +03006068 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006069 PyErr_Format(PyExc_TypeError,
6070 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006071 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006072 }
6073 argc = PySequence_Size(argv);
6074 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006075 PyErr_Format(PyExc_ValueError,
6076 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006077 return NULL;
6078 }
6079
6080 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006081 PyErr_Format(PyExc_TypeError,
6082 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006083 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006084 }
6085
6086 argvlist = parse_arglist(argv, &argc);
6087 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006088 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006089 }
6090 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01006091 PyErr_Format(PyExc_ValueError,
6092 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006093 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006094 }
6095
6096 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006097 if (envlist == NULL) {
6098 goto exit;
6099 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006100
Anthony Shaw948ed8c2019-05-10 12:00:06 +10006101 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01006102 /* There is a bug in old versions of glibc that makes some of the
6103 * helper functions for manipulating file actions not copy the provided
6104 * buffers. The problem is that posix_spawn_file_actions_addopen does not
6105 * copy the value of path for some old versions of glibc (<2.20).
6106 * The use of temp_buffer here is a workaround that keeps the
6107 * python objects that own the buffers alive until posix_spawn gets called.
6108 * Check https://bugs.python.org/issue33630 and
6109 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
6110 temp_buffer = PyList_New(0);
6111 if (!temp_buffer) {
6112 goto exit;
6113 }
6114 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006115 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006116 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03006117 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006118 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006119
Victor Stinner1c2fa782020-05-10 11:05:29 +02006120 if (parse_posix_spawn_flags(module, func_name, setpgroup, resetids, setsid,
Victor Stinner325e4ba2019-02-01 15:47:24 +01006121 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01006122 goto exit;
6123 }
6124 attrp = &attr;
6125
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006126 if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08006127 goto exit;
6128 }
6129
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006130 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006131#ifdef HAVE_POSIX_SPAWNP
6132 if (use_posix_spawnp) {
6133 err_code = posix_spawnp(&pid, path->narrow,
6134 file_actionsp, attrp, argvlist, envlist);
6135 }
6136 else
6137#endif /* HAVE_POSIX_SPAWNP */
6138 {
6139 err_code = posix_spawn(&pid, path->narrow,
6140 file_actionsp, attrp, argvlist, envlist);
6141 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006142 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006143
Serhiy Storchakaef347532018-05-01 16:45:04 +03006144 if (err_code) {
6145 errno = err_code;
6146 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006147 goto exit;
6148 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006149#ifdef _Py_MEMORY_SANITIZER
6150 __msan_unpoison(&pid, sizeof(pid));
6151#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006152 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006153
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006154exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03006155 if (file_actionsp) {
6156 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006157 }
Pablo Galindo254a4662018-09-07 16:44:24 +01006158 if (attrp) {
6159 (void)posix_spawnattr_destroy(attrp);
6160 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006161 if (envlist) {
6162 free_string_array(envlist, envc);
6163 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006164 if (argvlist) {
6165 free_string_array(argvlist, argc);
6166 }
Pablo Galindocb970732018-06-19 09:19:50 +01006167 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00006168 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006169}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006170
6171
6172/*[clinic input]
6173
6174os.posix_spawn
6175 path: path_t
6176 Path of executable file.
6177 argv: object
6178 Tuple or list of strings.
6179 env: object
6180 Dictionary of strings mapping to strings.
6181 /
6182 *
6183 file_actions: object(c_default='NULL') = ()
6184 A sequence of file action tuples.
6185 setpgroup: object = NULL
6186 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
6187 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006188 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
6189 setsid: bool(accept={int}) = False
6190 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006191 setsigmask: object(c_default='NULL') = ()
6192 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
6193 setsigdef: object(c_default='NULL') = ()
6194 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
6195 scheduler: object = NULL
6196 A tuple with the scheduler policy (optional) and parameters.
6197
6198Execute the program specified by path in a new process.
6199[clinic start generated code]*/
6200
6201static PyObject *
6202os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
6203 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006204 PyObject *setpgroup, int resetids, int setsid,
6205 PyObject *setsigmask, PyObject *setsigdef,
6206 PyObject *scheduler)
6207/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006208{
6209 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006210 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006211 scheduler);
6212}
6213 #endif /* HAVE_POSIX_SPAWN */
6214
6215
6216
6217#ifdef HAVE_POSIX_SPAWNP
6218/*[clinic input]
6219
6220os.posix_spawnp
6221 path: path_t
6222 Path of executable file.
6223 argv: object
6224 Tuple or list of strings.
6225 env: object
6226 Dictionary of strings mapping to strings.
6227 /
6228 *
6229 file_actions: object(c_default='NULL') = ()
6230 A sequence of file action tuples.
6231 setpgroup: object = NULL
6232 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
6233 resetids: bool(accept={int}) = False
6234 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006235 setsid: bool(accept={int}) = False
6236 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006237 setsigmask: object(c_default='NULL') = ()
6238 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
6239 setsigdef: object(c_default='NULL') = ()
6240 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
6241 scheduler: object = NULL
6242 A tuple with the scheduler policy (optional) and parameters.
6243
6244Execute the program specified by path in a new process.
6245[clinic start generated code]*/
6246
6247static PyObject *
6248os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
6249 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006250 PyObject *setpgroup, int resetids, int setsid,
6251 PyObject *setsigmask, PyObject *setsigdef,
6252 PyObject *scheduler)
6253/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006254{
6255 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03006256 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03006257 scheduler);
6258}
6259#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00006260
pxinwrf2d7ac72019-05-21 18:46:37 +08006261#ifdef HAVE_RTPSPAWN
6262static intptr_t
6263_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
6264 const char *envp[])
6265{
6266 RTP_ID rtpid;
6267 int status;
6268 pid_t res;
6269 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006270
pxinwrf2d7ac72019-05-21 18:46:37 +08006271 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
6272 uStackSize=0 cannot be used, the default stack size is too small for
6273 Python. */
6274 if (envp) {
6275 rtpid = rtpSpawn(rtpFileName, argv, envp,
6276 100, 0x1000000, 0, VX_FP_TASK);
6277 }
6278 else {
6279 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
6280 100, 0x1000000, 0, VX_FP_TASK);
6281 }
6282 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
6283 do {
6284 res = waitpid((pid_t)rtpid, &status, 0);
6285 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
6286
6287 if (res < 0)
6288 return RTP_ID_ERROR;
6289 return ((intptr_t)status);
6290 }
6291 return ((intptr_t)rtpid);
6292}
6293#endif
6294
6295#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10006296/*[clinic input]
6297os.spawnv
6298
6299 mode: int
6300 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07006301 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10006302 Path of executable file.
6303 argv: object
6304 Tuple or list of strings.
6305 /
6306
6307Execute the program specified by path in a new process.
6308[clinic start generated code]*/
6309
Larry Hastings2f936352014-08-05 14:04:04 +10006310static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07006311os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
6312/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006313{
Steve Dowercc16be82016-09-08 10:35:16 -07006314 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10006315 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00006316 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07006317 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00006318 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00006319
Victor Stinner8c62be82010-05-06 00:08:46 +00006320 /* spawnv has three arguments: (mode, path, argv), where
6321 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00006322
Victor Stinner8c62be82010-05-06 00:08:46 +00006323 if (PyList_Check(argv)) {
6324 argc = PyList_Size(argv);
6325 getitem = PyList_GetItem;
6326 }
6327 else if (PyTuple_Check(argv)) {
6328 argc = PyTuple_Size(argv);
6329 getitem = PyTuple_GetItem;
6330 }
6331 else {
6332 PyErr_SetString(PyExc_TypeError,
6333 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00006334 return NULL;
6335 }
Steve Dower859fd7b2016-11-19 18:53:19 -08006336 if (argc == 0) {
6337 PyErr_SetString(PyExc_ValueError,
6338 "spawnv() arg 2 cannot be empty");
6339 return NULL;
6340 }
Guido van Rossuma1065681999-01-25 23:20:23 +00006341
Steve Dowercc16be82016-09-08 10:35:16 -07006342 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00006343 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00006344 return PyErr_NoMemory();
6345 }
6346 for (i = 0; i < argc; i++) {
6347 if (!fsconvert_strdup((*getitem)(argv, i),
6348 &argvlist[i])) {
6349 free_string_array(argvlist, i);
6350 PyErr_SetString(
6351 PyExc_TypeError,
6352 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00006353 return NULL;
6354 }
Steve Dower93ff8722016-11-19 19:03:54 -08006355 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02006356 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08006357 PyErr_SetString(
6358 PyExc_ValueError,
6359 "spawnv() arg 2 first element cannot be empty");
6360 return NULL;
6361 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006362 }
6363 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00006364
pxinwrf2d7ac72019-05-21 18:46:37 +08006365#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006366 if (mode == _OLD_P_OVERLAY)
6367 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006368#endif
Tim Peters5aa91602002-01-30 05:46:57 +00006369
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006370 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
Saiyang Gou95f60012020-02-04 16:15:00 -08006371 Py_None) < 0) {
6372 free_string_array(argvlist, argc);
6373 return NULL;
6374 }
6375
Victor Stinner8c62be82010-05-06 00:08:46 +00006376 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006377 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006378#ifdef HAVE_WSPAWNV
6379 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006380#elif defined(HAVE_RTPSPAWN)
6381 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07006382#else
6383 spawnval = _spawnv(mode, path->narrow, argvlist);
6384#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006385 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006386 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00006387
Victor Stinner8c62be82010-05-06 00:08:46 +00006388 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00006389
Victor Stinner8c62be82010-05-06 00:08:46 +00006390 if (spawnval == -1)
6391 return posix_error();
6392 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006393 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006394}
6395
Larry Hastings2f936352014-08-05 14:04:04 +10006396/*[clinic input]
6397os.spawnve
6398
6399 mode: int
6400 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07006401 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10006402 Path of executable file.
6403 argv: object
6404 Tuple or list of strings.
6405 env: object
6406 Dictionary of strings mapping to strings.
6407 /
6408
6409Execute the program specified by path in a new process.
6410[clinic start generated code]*/
6411
Larry Hastings2f936352014-08-05 14:04:04 +10006412static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07006413os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006414 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07006415/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006416{
Steve Dowercc16be82016-09-08 10:35:16 -07006417 EXECV_CHAR **argvlist;
6418 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006419 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00006420 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07006421 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00006422 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006423 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00006424
Victor Stinner8c62be82010-05-06 00:08:46 +00006425 /* spawnve has four arguments: (mode, path, argv, env), where
6426 argv is a list or tuple of strings and env is a dictionary
6427 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00006428
Victor Stinner8c62be82010-05-06 00:08:46 +00006429 if (PyList_Check(argv)) {
6430 argc = PyList_Size(argv);
6431 getitem = PyList_GetItem;
6432 }
6433 else if (PyTuple_Check(argv)) {
6434 argc = PyTuple_Size(argv);
6435 getitem = PyTuple_GetItem;
6436 }
6437 else {
6438 PyErr_SetString(PyExc_TypeError,
6439 "spawnve() arg 2 must be a tuple or list");
6440 goto fail_0;
6441 }
Steve Dower859fd7b2016-11-19 18:53:19 -08006442 if (argc == 0) {
6443 PyErr_SetString(PyExc_ValueError,
6444 "spawnve() arg 2 cannot be empty");
6445 goto fail_0;
6446 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006447 if (!PyMapping_Check(env)) {
6448 PyErr_SetString(PyExc_TypeError,
6449 "spawnve() arg 3 must be a mapping object");
6450 goto fail_0;
6451 }
Guido van Rossuma1065681999-01-25 23:20:23 +00006452
Steve Dowercc16be82016-09-08 10:35:16 -07006453 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00006454 if (argvlist == NULL) {
6455 PyErr_NoMemory();
6456 goto fail_0;
6457 }
6458 for (i = 0; i < argc; i++) {
6459 if (!fsconvert_strdup((*getitem)(argv, i),
6460 &argvlist[i]))
6461 {
6462 lastarg = i;
6463 goto fail_1;
6464 }
Steve Dowerbce26262016-11-19 19:17:26 -08006465 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006466 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08006467 PyErr_SetString(
6468 PyExc_ValueError,
6469 "spawnv() arg 2 first element cannot be empty");
6470 goto fail_1;
6471 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006472 }
6473 lastarg = argc;
6474 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00006475
Victor Stinner8c62be82010-05-06 00:08:46 +00006476 envlist = parse_envlist(env, &envc);
6477 if (envlist == NULL)
6478 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00006479
pxinwrf2d7ac72019-05-21 18:46:37 +08006480#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006481 if (mode == _OLD_P_OVERLAY)
6482 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006483#endif
Tim Peters25059d32001-12-07 20:35:43 +00006484
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006485 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08006486 goto fail_2;
6487 }
6488
Victor Stinner8c62be82010-05-06 00:08:46 +00006489 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006490 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006491#ifdef HAVE_WSPAWNV
6492 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006493#elif defined(HAVE_RTPSPAWN)
6494 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6495 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07006496#else
6497 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6498#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006499 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006500 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00006501
Victor Stinner8c62be82010-05-06 00:08:46 +00006502 if (spawnval == -1)
6503 (void) posix_error();
6504 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006505 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006506
Saiyang Gou95f60012020-02-04 16:15:00 -08006507 fail_2:
Victor Stinner8c62be82010-05-06 00:08:46 +00006508 while (--envc >= 0)
6509 PyMem_DEL(envlist[envc]);
6510 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00006511 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006512 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00006513 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00006514 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00006515}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006516
Guido van Rossuma1065681999-01-25 23:20:23 +00006517#endif /* HAVE_SPAWNV */
6518
6519
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006520#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07006521
6522/* Helper function to validate arguments.
6523 Returns 0 on success. non-zero on failure with a TypeError raised.
6524 If obj is non-NULL it must be callable. */
6525static int
6526check_null_or_callable(PyObject *obj, const char* obj_name)
6527{
6528 if (obj && !PyCallable_Check(obj)) {
6529 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
Eddie Elizondob3966632019-11-05 07:16:14 -08006530 obj_name, _PyType_Name(Py_TYPE(obj)));
Gregory P. Smith163468a2017-05-29 10:03:41 -07006531 return -1;
6532 }
6533 return 0;
6534}
6535
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006536/*[clinic input]
6537os.register_at_fork
6538
Gregory P. Smith163468a2017-05-29 10:03:41 -07006539 *
6540 before: object=NULL
6541 A callable to be called in the parent before the fork() syscall.
6542 after_in_child: object=NULL
6543 A callable to be called in the child after fork().
6544 after_in_parent: object=NULL
6545 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006546
Gregory P. Smith163468a2017-05-29 10:03:41 -07006547Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006548
Gregory P. Smith163468a2017-05-29 10:03:41 -07006549'before' callbacks are called in reverse order.
6550'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006551
6552[clinic start generated code]*/
6553
6554static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006555os_register_at_fork_impl(PyObject *module, PyObject *before,
6556 PyObject *after_in_child, PyObject *after_in_parent)
6557/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006558{
6559 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006560
Gregory P. Smith163468a2017-05-29 10:03:41 -07006561 if (!before && !after_in_child && !after_in_parent) {
6562 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6563 return NULL;
6564 }
6565 if (check_null_or_callable(before, "before") ||
6566 check_null_or_callable(after_in_child, "after_in_child") ||
6567 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006568 return NULL;
6569 }
Victor Stinner81a7be32020-04-14 15:14:01 +02006570 interp = _PyInterpreterState_GET();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006571
Gregory P. Smith163468a2017-05-29 10:03:41 -07006572 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006573 return NULL;
6574 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006575 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006576 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006577 }
6578 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6579 return NULL;
6580 }
6581 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006582}
6583#endif /* HAVE_FORK */
6584
6585
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006586#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006587/*[clinic input]
6588os.fork1
6589
6590Fork a child process with a single multiplexed (i.e., not bound) thread.
6591
6592Return 0 to child process and PID of child to parent process.
6593[clinic start generated code]*/
6594
Larry Hastings2f936352014-08-05 14:04:04 +10006595static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006596os_fork1_impl(PyObject *module)
6597/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006598{
Victor Stinner8c62be82010-05-06 00:08:46 +00006599 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006600
Victor Stinner81a7be32020-04-14 15:14:01 +02006601 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006602 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6603 return NULL;
6604 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006605 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006606 pid = fork1();
6607 if (pid == 0) {
6608 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006609 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006610 } else {
6611 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006612 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006613 }
6614 if (pid == -1)
6615 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006616 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006617}
Larry Hastings2f936352014-08-05 14:04:04 +10006618#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006619
6620
Guido van Rossumad0ee831995-03-01 10:34:45 +00006621#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006622/*[clinic input]
6623os.fork
6624
6625Fork a child process.
6626
6627Return 0 to child process and PID of child to parent process.
6628[clinic start generated code]*/
6629
Larry Hastings2f936352014-08-05 14:04:04 +10006630static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006631os_fork_impl(PyObject *module)
6632/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006633{
Victor Stinner8c62be82010-05-06 00:08:46 +00006634 pid_t pid;
Victor Stinner252346a2020-05-01 11:33:44 +02006635 PyInterpreterState *interp = _PyInterpreterState_GET();
6636 if (interp->config._isolated_interpreter) {
6637 PyErr_SetString(PyExc_RuntimeError,
6638 "fork not supported for isolated subinterpreters");
Eric Snow59032962018-09-14 14:17:20 -07006639 return NULL;
6640 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006641 if (PySys_Audit("os.fork", NULL) < 0) {
6642 return NULL;
6643 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006644 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006645 pid = fork();
6646 if (pid == 0) {
6647 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006648 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006649 } else {
6650 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006651 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006652 }
6653 if (pid == -1)
6654 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006655 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006656}
Larry Hastings2f936352014-08-05 14:04:04 +10006657#endif /* HAVE_FORK */
6658
Guido van Rossum85e3b011991-06-03 12:42:10 +00006659
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006660#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006661#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006662/*[clinic input]
6663os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006664
Larry Hastings2f936352014-08-05 14:04:04 +10006665 policy: int
6666
6667Get the maximum scheduling priority for policy.
6668[clinic start generated code]*/
6669
Larry Hastings2f936352014-08-05 14:04:04 +10006670static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006671os_sched_get_priority_max_impl(PyObject *module, int policy)
6672/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006673{
6674 int max;
6675
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006676 max = sched_get_priority_max(policy);
6677 if (max < 0)
6678 return posix_error();
6679 return PyLong_FromLong(max);
6680}
6681
Larry Hastings2f936352014-08-05 14:04:04 +10006682
6683/*[clinic input]
6684os.sched_get_priority_min
6685
6686 policy: int
6687
6688Get the minimum scheduling priority for policy.
6689[clinic start generated code]*/
6690
Larry Hastings2f936352014-08-05 14:04:04 +10006691static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006692os_sched_get_priority_min_impl(PyObject *module, int policy)
6693/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006694{
6695 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006696 if (min < 0)
6697 return posix_error();
6698 return PyLong_FromLong(min);
6699}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006700#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6701
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006702
Larry Hastings2f936352014-08-05 14:04:04 +10006703#ifdef HAVE_SCHED_SETSCHEDULER
6704/*[clinic input]
6705os.sched_getscheduler
6706 pid: pid_t
6707 /
6708
Min ho Kimc4cacc82019-07-31 08:16:13 +10006709Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006710
6711Passing 0 for pid returns the scheduling policy for the calling process.
6712[clinic start generated code]*/
6713
Larry Hastings2f936352014-08-05 14:04:04 +10006714static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006715os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006716/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006717{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006718 int policy;
6719
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006720 policy = sched_getscheduler(pid);
6721 if (policy < 0)
6722 return posix_error();
6723 return PyLong_FromLong(policy);
6724}
Larry Hastings2f936352014-08-05 14:04:04 +10006725#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006726
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006727
William Orr81574b82018-10-01 22:19:56 -07006728#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006729/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006730class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006731
6732@classmethod
6733os.sched_param.__new__
6734
6735 sched_priority: object
6736 A scheduling parameter.
6737
Eddie Elizondob3966632019-11-05 07:16:14 -08006738Currently has only one field: sched_priority
Larry Hastings2f936352014-08-05 14:04:04 +10006739[clinic start generated code]*/
6740
Larry Hastings2f936352014-08-05 14:04:04 +10006741static PyObject *
6742os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondob3966632019-11-05 07:16:14 -08006743/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006744{
6745 PyObject *res;
6746
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006747 res = PyStructSequence_New(type);
6748 if (!res)
6749 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006750 Py_INCREF(sched_priority);
6751 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006752 return res;
6753}
6754
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006755PyDoc_VAR(os_sched_param__doc__);
6756
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006757static PyStructSequence_Field sched_param_fields[] = {
6758 {"sched_priority", "the scheduling priority"},
6759 {0}
6760};
6761
6762static PyStructSequence_Desc sched_param_desc = {
6763 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006764 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006765 sched_param_fields,
6766 1
6767};
6768
6769static int
Victor Stinner1c2fa782020-05-10 11:05:29 +02006770convert_sched_param(PyObject *module, PyObject *param, struct sched_param *res)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006771{
6772 long priority;
6773
Victor Stinner1c2fa782020-05-10 11:05:29 +02006774 if (!Py_IS_TYPE(param, (PyTypeObject *)get_posix_state(module)->SchedParamType)) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006775 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6776 return 0;
6777 }
6778 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6779 if (priority == -1 && PyErr_Occurred())
6780 return 0;
6781 if (priority > INT_MAX || priority < INT_MIN) {
6782 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6783 return 0;
6784 }
6785 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6786 return 1;
6787}
William Orr81574b82018-10-01 22:19:56 -07006788#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006789
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006790
6791#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006792/*[clinic input]
6793os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006794
Larry Hastings2f936352014-08-05 14:04:04 +10006795 pid: pid_t
6796 policy: int
Victor Stinner1c2fa782020-05-10 11:05:29 +02006797 param as param_obj: object
Larry Hastings2f936352014-08-05 14:04:04 +10006798 /
6799
6800Set the scheduling policy for the process identified by pid.
6801
6802If pid is 0, the calling process is changed.
6803param is an instance of sched_param.
6804[clinic start generated code]*/
6805
Larry Hastings2f936352014-08-05 14:04:04 +10006806static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006807os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Victor Stinner1c2fa782020-05-10 11:05:29 +02006808 PyObject *param_obj)
6809/*[clinic end generated code: output=cde27faa55dc993e input=73013d731bd8fbe9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006810{
Victor Stinner1c2fa782020-05-10 11:05:29 +02006811 struct sched_param param;
6812 if (!convert_sched_param(module, param_obj, &param)) {
6813 return NULL;
6814 }
6815
Jesus Cea9c822272011-09-10 01:40:52 +02006816 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006817 ** sched_setscheduler() returns 0 in Linux, but the previous
6818 ** scheduling policy under Solaris/Illumos, and others.
6819 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006820 */
Victor Stinner1c2fa782020-05-10 11:05:29 +02006821 if (sched_setscheduler(pid, policy, &param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006822 return posix_error();
6823 Py_RETURN_NONE;
6824}
Larry Hastings2f936352014-08-05 14:04:04 +10006825#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006826
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006827
6828#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006829/*[clinic input]
6830os.sched_getparam
6831 pid: pid_t
6832 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006833
Larry Hastings2f936352014-08-05 14:04:04 +10006834Returns scheduling parameters for the process identified by pid.
6835
6836If pid is 0, returns parameters for the calling process.
6837Return value is an instance of sched_param.
6838[clinic start generated code]*/
6839
Larry Hastings2f936352014-08-05 14:04:04 +10006840static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006841os_sched_getparam_impl(PyObject *module, pid_t pid)
6842/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006843{
6844 struct sched_param param;
6845 PyObject *result;
6846 PyObject *priority;
6847
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006848 if (sched_getparam(pid, &param))
6849 return posix_error();
Victor Stinner1c2fa782020-05-10 11:05:29 +02006850 PyObject *SchedParamType = get_posix_state(module)->SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -08006851 result = PyStructSequence_New((PyTypeObject *)SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006852 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006853 return NULL;
6854 priority = PyLong_FromLong(param.sched_priority);
6855 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006856 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006857 return NULL;
6858 }
Larry Hastings2f936352014-08-05 14:04:04 +10006859 PyStructSequence_SET_ITEM(result, 0, priority);
6860 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006861}
6862
Larry Hastings2f936352014-08-05 14:04:04 +10006863
6864/*[clinic input]
6865os.sched_setparam
6866 pid: pid_t
Victor Stinner1c2fa782020-05-10 11:05:29 +02006867 param as param_obj: object
Larry Hastings2f936352014-08-05 14:04:04 +10006868 /
6869
6870Set scheduling parameters for the process identified by pid.
6871
6872If pid is 0, sets parameters for the calling process.
6873param should be an instance of sched_param.
6874[clinic start generated code]*/
6875
Larry Hastings2f936352014-08-05 14:04:04 +10006876static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02006877os_sched_setparam_impl(PyObject *module, pid_t pid, PyObject *param_obj)
6878/*[clinic end generated code: output=f19fe020a53741c1 input=27b98337c8b2dcc7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006879{
Victor Stinner1c2fa782020-05-10 11:05:29 +02006880 struct sched_param param;
6881 if (!convert_sched_param(module, param_obj, &param)) {
6882 return NULL;
6883 }
6884
6885 if (sched_setparam(pid, &param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006886 return posix_error();
6887 Py_RETURN_NONE;
6888}
Larry Hastings2f936352014-08-05 14:04:04 +10006889#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006890
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006891
6892#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006893/*[clinic input]
6894os.sched_rr_get_interval -> double
6895 pid: pid_t
6896 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006897
Larry Hastings2f936352014-08-05 14:04:04 +10006898Return the round-robin quantum for the process identified by pid, in seconds.
6899
6900Value returned is a float.
6901[clinic start generated code]*/
6902
Larry Hastings2f936352014-08-05 14:04:04 +10006903static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006904os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6905/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006906{
6907 struct timespec interval;
6908 if (sched_rr_get_interval(pid, &interval)) {
6909 posix_error();
6910 return -1.0;
6911 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006912#ifdef _Py_MEMORY_SANITIZER
6913 __msan_unpoison(&interval, sizeof(interval));
6914#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006915 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6916}
6917#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006918
Larry Hastings2f936352014-08-05 14:04:04 +10006919
6920/*[clinic input]
6921os.sched_yield
6922
6923Voluntarily relinquish the CPU.
6924[clinic start generated code]*/
6925
Larry Hastings2f936352014-08-05 14:04:04 +10006926static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006927os_sched_yield_impl(PyObject *module)
6928/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006929{
6930 if (sched_yield())
6931 return posix_error();
6932 Py_RETURN_NONE;
6933}
6934
Benjamin Peterson2740af82011-08-02 17:41:34 -05006935#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006936/* The minimum number of CPUs allocated in a cpu_set_t */
6937static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006938
Larry Hastings2f936352014-08-05 14:04:04 +10006939/*[clinic input]
6940os.sched_setaffinity
6941 pid: pid_t
6942 mask : object
6943 /
6944
6945Set the CPU affinity of the process identified by pid to mask.
6946
6947mask should be an iterable of integers identifying CPUs.
6948[clinic start generated code]*/
6949
Larry Hastings2f936352014-08-05 14:04:04 +10006950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006951os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6952/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006953{
Antoine Pitrou84869872012-08-04 16:16:35 +02006954 int ncpus;
6955 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006956 cpu_set_t *cpu_set = NULL;
6957 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006958
Larry Hastings2f936352014-08-05 14:04:04 +10006959 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006960 if (iterator == NULL)
6961 return NULL;
6962
6963 ncpus = NCPUS_START;
6964 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006965 cpu_set = CPU_ALLOC(ncpus);
6966 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006967 PyErr_NoMemory();
6968 goto error;
6969 }
Larry Hastings2f936352014-08-05 14:04:04 +10006970 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006971
6972 while ((item = PyIter_Next(iterator))) {
6973 long cpu;
6974 if (!PyLong_Check(item)) {
6975 PyErr_Format(PyExc_TypeError,
6976 "expected an iterator of ints, "
6977 "but iterator yielded %R",
6978 Py_TYPE(item));
6979 Py_DECREF(item);
6980 goto error;
6981 }
6982 cpu = PyLong_AsLong(item);
6983 Py_DECREF(item);
6984 if (cpu < 0) {
6985 if (!PyErr_Occurred())
6986 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6987 goto error;
6988 }
6989 if (cpu > INT_MAX - 1) {
6990 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6991 goto error;
6992 }
6993 if (cpu >= ncpus) {
6994 /* Grow CPU mask to fit the CPU number */
6995 int newncpus = ncpus;
6996 cpu_set_t *newmask;
6997 size_t newsetsize;
6998 while (newncpus <= cpu) {
6999 if (newncpus > INT_MAX / 2)
7000 newncpus = cpu + 1;
7001 else
7002 newncpus = newncpus * 2;
7003 }
7004 newmask = CPU_ALLOC(newncpus);
7005 if (newmask == NULL) {
7006 PyErr_NoMemory();
7007 goto error;
7008 }
7009 newsetsize = CPU_ALLOC_SIZE(newncpus);
7010 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10007011 memcpy(newmask, cpu_set, setsize);
7012 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02007013 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10007014 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02007015 ncpus = newncpus;
7016 }
Larry Hastings2f936352014-08-05 14:04:04 +10007017 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02007018 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07007019 if (PyErr_Occurred()) {
7020 goto error;
7021 }
Antoine Pitrou84869872012-08-04 16:16:35 +02007022 Py_CLEAR(iterator);
7023
Larry Hastings2f936352014-08-05 14:04:04 +10007024 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02007025 posix_error();
7026 goto error;
7027 }
Larry Hastings2f936352014-08-05 14:04:04 +10007028 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007029 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02007030
7031error:
Larry Hastings2f936352014-08-05 14:04:04 +10007032 if (cpu_set)
7033 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02007034 Py_XDECREF(iterator);
7035 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007036}
7037
Larry Hastings2f936352014-08-05 14:04:04 +10007038
7039/*[clinic input]
7040os.sched_getaffinity
7041 pid: pid_t
7042 /
7043
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01007044Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10007045
7046The affinity is returned as a set of CPU identifiers.
7047[clinic start generated code]*/
7048
Larry Hastings2f936352014-08-05 14:04:04 +10007049static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007050os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03007051/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007052{
Antoine Pitrou84869872012-08-04 16:16:35 +02007053 int cpu, ncpus, count;
7054 size_t setsize;
7055 cpu_set_t *mask = NULL;
7056 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007057
Antoine Pitrou84869872012-08-04 16:16:35 +02007058 ncpus = NCPUS_START;
7059 while (1) {
7060 setsize = CPU_ALLOC_SIZE(ncpus);
7061 mask = CPU_ALLOC(ncpus);
7062 if (mask == NULL)
7063 return PyErr_NoMemory();
7064 if (sched_getaffinity(pid, setsize, mask) == 0)
7065 break;
7066 CPU_FREE(mask);
7067 if (errno != EINVAL)
7068 return posix_error();
7069 if (ncpus > INT_MAX / 2) {
7070 PyErr_SetString(PyExc_OverflowError, "could not allocate "
7071 "a large enough CPU set");
7072 return NULL;
7073 }
7074 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007075 }
Antoine Pitrou84869872012-08-04 16:16:35 +02007076
7077 res = PySet_New(NULL);
7078 if (res == NULL)
7079 goto error;
7080 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
7081 if (CPU_ISSET_S(cpu, setsize, mask)) {
7082 PyObject *cpu_num = PyLong_FromLong(cpu);
7083 --count;
7084 if (cpu_num == NULL)
7085 goto error;
7086 if (PySet_Add(res, cpu_num)) {
7087 Py_DECREF(cpu_num);
7088 goto error;
7089 }
7090 Py_DECREF(cpu_num);
7091 }
7092 }
7093 CPU_FREE(mask);
7094 return res;
7095
7096error:
7097 if (mask)
7098 CPU_FREE(mask);
7099 Py_XDECREF(res);
7100 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007101}
7102
Benjamin Peterson2740af82011-08-02 17:41:34 -05007103#endif /* HAVE_SCHED_SETAFFINITY */
7104
Benjamin Peterson94b580d2011-08-02 17:30:04 -05007105#endif /* HAVE_SCHED_H */
7106
Larry Hastings2f936352014-08-05 14:04:04 +10007107
Neal Norwitzb59798b2003-03-21 01:43:31 +00007108/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00007109#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Victor Stinner57766632020-10-29 15:16:23 +01007110# define DEV_PTY_FILE "/dev/ptc"
7111# define HAVE_DEV_PTMX
Neal Norwitzb59798b2003-03-21 01:43:31 +00007112#else
Victor Stinner57766632020-10-29 15:16:23 +01007113# define DEV_PTY_FILE "/dev/ptmx"
Neal Norwitzb59798b2003-03-21 01:43:31 +00007114#endif
7115
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007116#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00007117#ifdef HAVE_PTY_H
7118#include <pty.h>
7119#else
7120#ifdef HAVE_LIBUTIL_H
7121#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00007122#else
7123#ifdef HAVE_UTIL_H
7124#include <util.h>
7125#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007126#endif /* HAVE_LIBUTIL_H */
7127#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00007128#ifdef HAVE_STROPTS_H
7129#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007130#endif
ngie-eign7745ec42018-02-14 11:54:28 -08007131#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007132
Larry Hastings2f936352014-08-05 14:04:04 +10007133
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007134#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10007135/*[clinic input]
7136os.openpty
7137
7138Open a pseudo-terminal.
7139
7140Return a tuple of (master_fd, slave_fd) containing open file descriptors
7141for both the master and slave ends.
7142[clinic start generated code]*/
7143
Larry Hastings2f936352014-08-05 14:04:04 +10007144static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007145os_openpty_impl(PyObject *module)
7146/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00007147{
Victor Stinnerdaf45552013-08-28 00:53:59 +02007148 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00007149#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007150 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00007151#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007152#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00007153 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01007154#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00007155 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007156#endif
7157#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00007158
Thomas Wouters70c21a12000-07-14 14:28:33 +00007159#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00007160 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007161 goto posix_error;
7162
7163 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
7164 goto error;
7165 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
7166 goto error;
7167
Neal Norwitzb59798b2003-03-21 01:43:31 +00007168#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00007169 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
7170 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007171 goto posix_error;
7172 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
7173 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00007174
Victor Stinnerdaf45552013-08-28 00:53:59 +02007175 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00007176 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01007177 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02007178
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007179#else
Victor Stinner000de532013-11-25 23:19:58 +01007180 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00007181 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007182 goto posix_error;
7183
Victor Stinner8c62be82010-05-06 00:08:46 +00007184 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007185
Victor Stinner8c62be82010-05-06 00:08:46 +00007186 /* change permission of slave */
7187 if (grantpt(master_fd) < 0) {
7188 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007189 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007190 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007191
Victor Stinner8c62be82010-05-06 00:08:46 +00007192 /* unlock slave */
7193 if (unlockpt(master_fd) < 0) {
7194 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007195 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00007196 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02007197
Victor Stinner8c62be82010-05-06 00:08:46 +00007198 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02007199
Victor Stinner8c62be82010-05-06 00:08:46 +00007200 slave_name = ptsname(master_fd); /* get name of slave */
7201 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02007202 goto posix_error;
7203
7204 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01007205 if (slave_fd == -1)
7206 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01007207
7208 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
7209 goto posix_error;
7210
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02007211#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00007212 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
7213 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00007214#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00007215 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00007216#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007217#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00007218#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00007219
Victor Stinner8c62be82010-05-06 00:08:46 +00007220 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00007221
Victor Stinnerdaf45552013-08-28 00:53:59 +02007222posix_error:
7223 posix_error();
7224error:
7225 if (master_fd != -1)
7226 close(master_fd);
7227 if (slave_fd != -1)
7228 close(slave_fd);
7229 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00007230}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00007231#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00007232
Larry Hastings2f936352014-08-05 14:04:04 +10007233
Fred Drake8cef4cf2000-06-28 16:40:38 +00007234#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10007235/*[clinic input]
7236os.forkpty
7237
7238Fork a new process with a new pseudo-terminal as controlling tty.
7239
7240Returns a tuple of (pid, master_fd).
7241Like fork(), return pid of 0 to the child process,
7242and pid of child to the parent process.
7243To both, return fd of newly opened pseudo-terminal.
7244[clinic start generated code]*/
7245
Larry Hastings2f936352014-08-05 14:04:04 +10007246static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007247os_forkpty_impl(PyObject *module)
7248/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00007249{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007250 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00007251 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00007252
Victor Stinner81a7be32020-04-14 15:14:01 +02007253 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07007254 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
7255 return NULL;
7256 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007257 if (PySys_Audit("os.forkpty", NULL) < 0) {
7258 return NULL;
7259 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007260 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00007261 pid = forkpty(&master_fd, NULL, NULL, NULL);
7262 if (pid == 0) {
7263 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007264 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00007265 } else {
7266 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02007267 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00007268 }
7269 if (pid == -1)
7270 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00007271 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00007272}
Larry Hastings2f936352014-08-05 14:04:04 +10007273#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007274
Ross Lagerwall7807c352011-03-17 20:20:30 +02007275
Guido van Rossumad0ee831995-03-01 10:34:45 +00007276#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007277/*[clinic input]
7278os.getegid
7279
7280Return the current process's effective group id.
7281[clinic start generated code]*/
7282
Larry Hastings2f936352014-08-05 14:04:04 +10007283static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007284os_getegid_impl(PyObject *module)
7285/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007286{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007287 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007288}
Larry Hastings2f936352014-08-05 14:04:04 +10007289#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007290
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007291
Guido van Rossumad0ee831995-03-01 10:34:45 +00007292#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007293/*[clinic input]
7294os.geteuid
7295
7296Return the current process's effective user id.
7297[clinic start generated code]*/
7298
Larry Hastings2f936352014-08-05 14:04:04 +10007299static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007300os_geteuid_impl(PyObject *module)
7301/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007302{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007303 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007304}
Larry Hastings2f936352014-08-05 14:04:04 +10007305#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007306
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007307
Guido van Rossumad0ee831995-03-01 10:34:45 +00007308#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007309/*[clinic input]
7310os.getgid
7311
7312Return the current process's group id.
7313[clinic start generated code]*/
7314
Larry Hastings2f936352014-08-05 14:04:04 +10007315static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007316os_getgid_impl(PyObject *module)
7317/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007318{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007319 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007320}
Larry Hastings2f936352014-08-05 14:04:04 +10007321#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007322
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007323
Berker Peksag39404992016-09-15 20:45:16 +03007324#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10007325/*[clinic input]
7326os.getpid
7327
7328Return the current process id.
7329[clinic start generated code]*/
7330
Larry Hastings2f936352014-08-05 14:04:04 +10007331static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007332os_getpid_impl(PyObject *module)
7333/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007334{
Victor Stinner8c62be82010-05-06 00:08:46 +00007335 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00007336}
Berker Peksag39404992016-09-15 20:45:16 +03007337#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007338
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07007339#ifdef NGROUPS_MAX
7340#define MAX_GROUPS NGROUPS_MAX
7341#else
7342 /* defined to be 16 on Solaris7, so this should be a small number */
7343#define MAX_GROUPS 64
7344#endif
7345
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007346#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10007347
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007348#ifdef __APPLE__
7349/*[clinic input]
7350os.getgrouplist
7351
7352 user: str
7353 username to lookup
7354 group as basegid: int
7355 base group id of the user
7356 /
7357
7358Returns a list of groups to which a user belongs.
7359[clinic start generated code]*/
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007360
7361static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007362os_getgrouplist_impl(PyObject *module, const char *user, int basegid)
7363/*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/
7364#else
7365/*[clinic input]
7366os.getgrouplist
7367
7368 user: str
7369 username to lookup
7370 group as basegid: gid_t
7371 base group id of the user
7372 /
7373
7374Returns a list of groups to which a user belongs.
7375[clinic start generated code]*/
7376
7377static PyObject *
7378os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid)
7379/*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/
7380#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007381{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007382 int i, ngroups;
7383 PyObject *list;
7384#ifdef __APPLE__
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007385 int *groups;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007386#else
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007387 gid_t *groups;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007388#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07007389
7390 /*
7391 * NGROUPS_MAX is defined by POSIX.1 as the maximum
7392 * number of supplimental groups a users can belong to.
7393 * We have to increment it by one because
7394 * getgrouplist() returns both the supplemental groups
7395 * and the primary group, i.e. all of the groups the
7396 * user belongs to.
7397 */
7398 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007399
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007400 while (1) {
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007401#ifdef __APPLE__
Victor Stinner8ec73702020-03-23 20:00:57 +01007402 groups = PyMem_New(int, ngroups);
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007403#else
7404 groups = PyMem_New(gid_t, ngroups);
7405#endif
Victor Stinner8ec73702020-03-23 20:00:57 +01007406 if (groups == NULL) {
7407 return PyErr_NoMemory();
7408 }
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007409
7410 int old_ngroups = ngroups;
7411 if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
7412 /* Success */
7413 break;
7414 }
7415
7416 /* getgrouplist() fails if the group list is too small */
7417 PyMem_Free(groups);
7418
7419 if (ngroups > old_ngroups) {
7420 /* If the group list is too small, the glibc implementation of
7421 getgrouplist() sets ngroups to the total number of groups and
7422 returns -1. */
7423 }
7424 else {
7425 /* Double the group list size */
7426 if (ngroups > INT_MAX / 2) {
7427 return PyErr_NoMemory();
7428 }
7429 ngroups *= 2;
7430 }
7431
7432 /* Retry getgrouplist() with a larger group list */
Victor Stinner8ec73702020-03-23 20:00:57 +01007433 }
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007434
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08007435#ifdef _Py_MEMORY_SANITIZER
7436 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
7437 __msan_unpoison(&ngroups, sizeof(ngroups));
7438 __msan_unpoison(groups, ngroups*sizeof(*groups));
7439#endif
7440
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007441 list = PyList_New(ngroups);
7442 if (list == NULL) {
7443 PyMem_Del(groups);
7444 return NULL;
7445 }
7446
7447 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007448#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007449 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007450#else
7451 PyObject *o = _PyLong_FromGid(groups[i]);
7452#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007453 if (o == NULL) {
7454 Py_DECREF(list);
7455 PyMem_Del(groups);
7456 return NULL;
7457 }
7458 PyList_SET_ITEM(list, i, o);
7459 }
7460
7461 PyMem_Del(groups);
7462
7463 return list;
7464}
Larry Hastings2f936352014-08-05 14:04:04 +10007465#endif /* HAVE_GETGROUPLIST */
7466
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007467
Fred Drakec9680921999-12-13 16:37:25 +00007468#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007469/*[clinic input]
7470os.getgroups
7471
7472Return list of supplemental group IDs for the process.
7473[clinic start generated code]*/
7474
Larry Hastings2f936352014-08-05 14:04:04 +10007475static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007476os_getgroups_impl(PyObject *module)
7477/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00007478{
7479 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007480 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007481
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007482 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007483 * This is a helper variable to store the intermediate result when
7484 * that happens.
7485 *
7486 * To keep the code readable the OSX behaviour is unconditional,
7487 * according to the POSIX spec this should be safe on all unix-y
7488 * systems.
7489 */
7490 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00007491 int n;
Fred Drakec9680921999-12-13 16:37:25 +00007492
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007493#ifdef __APPLE__
7494 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
7495 * there are more groups than can fit in grouplist. Therefore, on OS X
7496 * always first call getgroups with length 0 to get the actual number
7497 * of groups.
7498 */
7499 n = getgroups(0, NULL);
7500 if (n < 0) {
7501 return posix_error();
7502 } else if (n <= MAX_GROUPS) {
7503 /* groups will fit in existing array */
7504 alt_grouplist = grouplist;
7505 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007506 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007507 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007508 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007509 }
7510 }
7511
7512 n = getgroups(n, alt_grouplist);
7513 if (n == -1) {
7514 if (alt_grouplist != grouplist) {
7515 PyMem_Free(alt_grouplist);
7516 }
7517 return posix_error();
7518 }
7519#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007521 if (n < 0) {
7522 if (errno == EINVAL) {
7523 n = getgroups(0, NULL);
7524 if (n == -1) {
7525 return posix_error();
7526 }
7527 if (n == 0) {
7528 /* Avoid malloc(0) */
7529 alt_grouplist = grouplist;
7530 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007531 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007532 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007533 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007534 }
7535 n = getgroups(n, alt_grouplist);
7536 if (n == -1) {
7537 PyMem_Free(alt_grouplist);
7538 return posix_error();
7539 }
7540 }
7541 } else {
7542 return posix_error();
7543 }
7544 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007545#endif
7546
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007547 result = PyList_New(n);
7548 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007549 int i;
7550 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007551 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00007552 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007553 Py_DECREF(result);
7554 result = NULL;
7555 break;
Fred Drakec9680921999-12-13 16:37:25 +00007556 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007557 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00007558 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007559 }
7560
7561 if (alt_grouplist != grouplist) {
7562 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 }
Neal Norwitze241ce82003-02-17 18:17:05 +00007564
Fred Drakec9680921999-12-13 16:37:25 +00007565 return result;
7566}
Larry Hastings2f936352014-08-05 14:04:04 +10007567#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00007568
Antoine Pitroub7572f02009-12-02 20:46:48 +00007569#ifdef HAVE_INITGROUPS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007570#ifdef __APPLE__
7571/*[clinic input]
7572os.initgroups
Antoine Pitroub7572f02009-12-02 20:46:48 +00007573
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007574 username as oname: FSConverter
7575 gid: int
7576 /
7577
7578Initialize the group access list.
7579
7580Call the system initgroups() to initialize the group access list with all of
7581the groups of which the specified username is a member, plus the specified
7582group id.
7583[clinic start generated code]*/
7584
Antoine Pitroub7572f02009-12-02 20:46:48 +00007585static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007586os_initgroups_impl(PyObject *module, PyObject *oname, int gid)
7587/*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/
7588#else
7589/*[clinic input]
7590os.initgroups
7591
7592 username as oname: FSConverter
7593 gid: gid_t
7594 /
7595
7596Initialize the group access list.
7597
7598Call the system initgroups() to initialize the group access list with all of
7599the groups of which the specified username is a member, plus the specified
7600group id.
7601[clinic start generated code]*/
7602
7603static PyObject *
7604os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid)
7605/*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/
7606#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007607{
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007608 const char *username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007609
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007610 if (initgroups(username, gid) == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007611 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007612
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007613 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007614}
Larry Hastings2f936352014-08-05 14:04:04 +10007615#endif /* HAVE_INITGROUPS */
7616
Antoine Pitroub7572f02009-12-02 20:46:48 +00007617
Martin v. Löwis606edc12002-06-13 21:09:11 +00007618#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007619/*[clinic input]
7620os.getpgid
7621
7622 pid: pid_t
7623
7624Call the system call getpgid(), and return the result.
7625[clinic start generated code]*/
7626
Larry Hastings2f936352014-08-05 14:04:04 +10007627static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007628os_getpgid_impl(PyObject *module, pid_t pid)
7629/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007630{
7631 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007632 if (pgid < 0)
7633 return posix_error();
7634 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007635}
7636#endif /* HAVE_GETPGID */
7637
7638
Guido van Rossumb6775db1994-08-01 11:34:53 +00007639#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007640/*[clinic input]
7641os.getpgrp
7642
7643Return the current process group id.
7644[clinic start generated code]*/
7645
Larry Hastings2f936352014-08-05 14:04:04 +10007646static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007647os_getpgrp_impl(PyObject *module)
7648/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007649{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007650#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007651 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007652#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007653 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007654#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007655}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007656#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007657
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007658
Guido van Rossumb6775db1994-08-01 11:34:53 +00007659#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007660/*[clinic input]
7661os.setpgrp
7662
7663Make the current process the leader of its process group.
7664[clinic start generated code]*/
7665
Larry Hastings2f936352014-08-05 14:04:04 +10007666static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007667os_setpgrp_impl(PyObject *module)
7668/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007669{
Guido van Rossum64933891994-10-20 21:56:42 +00007670#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007671 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007672#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007673 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007674#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007675 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007676 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007677}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007678#endif /* HAVE_SETPGRP */
7679
Guido van Rossumad0ee831995-03-01 10:34:45 +00007680#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007681
7682#ifdef MS_WINDOWS
7683#include <tlhelp32.h>
7684
7685static PyObject*
7686win32_getppid()
7687{
7688 HANDLE snapshot;
7689 pid_t mypid;
7690 PyObject* result = NULL;
7691 BOOL have_record;
7692 PROCESSENTRY32 pe;
7693
7694 mypid = getpid(); /* This function never fails */
7695
7696 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7697 if (snapshot == INVALID_HANDLE_VALUE)
7698 return PyErr_SetFromWindowsErr(GetLastError());
7699
7700 pe.dwSize = sizeof(pe);
7701 have_record = Process32First(snapshot, &pe);
7702 while (have_record) {
7703 if (mypid == (pid_t)pe.th32ProcessID) {
7704 /* We could cache the ulong value in a static variable. */
7705 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7706 break;
7707 }
7708
7709 have_record = Process32Next(snapshot, &pe);
7710 }
7711
7712 /* If our loop exits and our pid was not found (result will be NULL)
7713 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7714 * error anyway, so let's raise it. */
7715 if (!result)
7716 result = PyErr_SetFromWindowsErr(GetLastError());
7717
7718 CloseHandle(snapshot);
7719
7720 return result;
7721}
7722#endif /*MS_WINDOWS*/
7723
Larry Hastings2f936352014-08-05 14:04:04 +10007724
7725/*[clinic input]
7726os.getppid
7727
7728Return the parent's process id.
7729
7730If the parent process has already exited, Windows machines will still
7731return its id; others systems will return the id of the 'init' process (1).
7732[clinic start generated code]*/
7733
Larry Hastings2f936352014-08-05 14:04:04 +10007734static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007735os_getppid_impl(PyObject *module)
7736/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007737{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007738#ifdef MS_WINDOWS
7739 return win32_getppid();
7740#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007741 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007742#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007743}
7744#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007745
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007746
Fred Drake12c6e2d1999-12-14 21:25:03 +00007747#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007748/*[clinic input]
7749os.getlogin
7750
7751Return the actual login name.
7752[clinic start generated code]*/
7753
Larry Hastings2f936352014-08-05 14:04:04 +10007754static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007755os_getlogin_impl(PyObject *module)
7756/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007757{
Victor Stinner8c62be82010-05-06 00:08:46 +00007758 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007759#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007760 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007761 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007762
7763 if (GetUserNameW(user_name, &num_chars)) {
7764 /* num_chars is the number of unicode chars plus null terminator */
7765 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007766 }
7767 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007768 result = PyErr_SetFromWindowsErr(GetLastError());
7769#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007770 char *name;
7771 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007772
Victor Stinner8c62be82010-05-06 00:08:46 +00007773 errno = 0;
7774 name = getlogin();
7775 if (name == NULL) {
7776 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007777 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007778 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007779 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007780 }
7781 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007782 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007783 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007784#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007785 return result;
7786}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007787#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007788
Larry Hastings2f936352014-08-05 14:04:04 +10007789
Guido van Rossumad0ee831995-03-01 10:34:45 +00007790#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007791/*[clinic input]
7792os.getuid
7793
7794Return the current process's user id.
7795[clinic start generated code]*/
7796
Larry Hastings2f936352014-08-05 14:04:04 +10007797static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007798os_getuid_impl(PyObject *module)
7799/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007800{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007801 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007802}
Larry Hastings2f936352014-08-05 14:04:04 +10007803#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007804
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007805
Brian Curtineb24d742010-04-12 17:16:38 +00007806#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007807#define HAVE_KILL
7808#endif /* MS_WINDOWS */
7809
7810#ifdef HAVE_KILL
7811/*[clinic input]
7812os.kill
7813
7814 pid: pid_t
7815 signal: Py_ssize_t
7816 /
7817
7818Kill a process with a signal.
7819[clinic start generated code]*/
7820
Larry Hastings2f936352014-08-05 14:04:04 +10007821static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007822os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7823/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007824{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007825 if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
7826 return NULL;
7827 }
7828#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007829 if (kill(pid, (int)signal) == -1)
7830 return posix_error();
7831 Py_RETURN_NONE;
Larry Hastings2f936352014-08-05 14:04:04 +10007832#else /* !MS_WINDOWS */
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007833 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007834 DWORD sig = (DWORD)signal;
7835 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007836 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007837
Victor Stinner8c62be82010-05-06 00:08:46 +00007838 /* Console processes which share a common console can be sent CTRL+C or
7839 CTRL+BREAK events, provided they handle said events. */
7840 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007841 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007842 err = GetLastError();
7843 PyErr_SetFromWindowsErr(err);
7844 }
7845 else
7846 Py_RETURN_NONE;
7847 }
Brian Curtineb24d742010-04-12 17:16:38 +00007848
Victor Stinner8c62be82010-05-06 00:08:46 +00007849 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7850 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007851 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007852 if (handle == NULL) {
7853 err = GetLastError();
7854 return PyErr_SetFromWindowsErr(err);
7855 }
Brian Curtineb24d742010-04-12 17:16:38 +00007856
Victor Stinner8c62be82010-05-06 00:08:46 +00007857 if (TerminateProcess(handle, sig) == 0) {
7858 err = GetLastError();
7859 result = PyErr_SetFromWindowsErr(err);
7860 } else {
7861 Py_INCREF(Py_None);
7862 result = Py_None;
7863 }
Brian Curtineb24d742010-04-12 17:16:38 +00007864
Victor Stinner8c62be82010-05-06 00:08:46 +00007865 CloseHandle(handle);
7866 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10007867#endif /* !MS_WINDOWS */
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007868}
Larry Hastings2f936352014-08-05 14:04:04 +10007869#endif /* HAVE_KILL */
7870
7871
7872#ifdef HAVE_KILLPG
7873/*[clinic input]
7874os.killpg
7875
7876 pgid: pid_t
7877 signal: int
7878 /
7879
7880Kill a process group with a signal.
7881[clinic start generated code]*/
7882
Larry Hastings2f936352014-08-05 14:04:04 +10007883static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007884os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7885/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007886{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007887 if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
7888 return NULL;
7889 }
Larry Hastings2f936352014-08-05 14:04:04 +10007890 /* XXX some man pages make the `pgid` parameter an int, others
7891 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7892 take the same type. Moreover, pid_t is always at least as wide as
7893 int (else compilation of this module fails), which is safe. */
7894 if (killpg(pgid, signal) == -1)
7895 return posix_error();
7896 Py_RETURN_NONE;
7897}
7898#endif /* HAVE_KILLPG */
7899
Brian Curtineb24d742010-04-12 17:16:38 +00007900
Guido van Rossumc0125471996-06-28 18:55:32 +00007901#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007902#ifdef HAVE_SYS_LOCK_H
7903#include <sys/lock.h>
7904#endif
7905
Larry Hastings2f936352014-08-05 14:04:04 +10007906/*[clinic input]
7907os.plock
7908 op: int
7909 /
7910
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007911Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007912[clinic start generated code]*/
7913
Larry Hastings2f936352014-08-05 14:04:04 +10007914static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007915os_plock_impl(PyObject *module, int op)
7916/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007917{
Victor Stinner8c62be82010-05-06 00:08:46 +00007918 if (plock(op) == -1)
7919 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007920 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007921}
Larry Hastings2f936352014-08-05 14:04:04 +10007922#endif /* HAVE_PLOCK */
7923
Guido van Rossumc0125471996-06-28 18:55:32 +00007924
Guido van Rossumb6775db1994-08-01 11:34:53 +00007925#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007926/*[clinic input]
7927os.setuid
7928
7929 uid: uid_t
7930 /
7931
7932Set the current process's user id.
7933[clinic start generated code]*/
7934
Larry Hastings2f936352014-08-05 14:04:04 +10007935static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007936os_setuid_impl(PyObject *module, uid_t uid)
7937/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007938{
Victor Stinner8c62be82010-05-06 00:08:46 +00007939 if (setuid(uid) < 0)
7940 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007941 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007942}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007943#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007944
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007945
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007946#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007947/*[clinic input]
7948os.seteuid
7949
7950 euid: uid_t
7951 /
7952
7953Set the current process's effective user id.
7954[clinic start generated code]*/
7955
Larry Hastings2f936352014-08-05 14:04:04 +10007956static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007957os_seteuid_impl(PyObject *module, uid_t euid)
7958/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007959{
7960 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007961 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007962 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007963}
7964#endif /* HAVE_SETEUID */
7965
Larry Hastings2f936352014-08-05 14:04:04 +10007966
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007967#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007968/*[clinic input]
7969os.setegid
7970
7971 egid: gid_t
7972 /
7973
7974Set the current process's effective group id.
7975[clinic start generated code]*/
7976
Larry Hastings2f936352014-08-05 14:04:04 +10007977static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007978os_setegid_impl(PyObject *module, gid_t egid)
7979/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007980{
7981 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007982 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007983 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007984}
7985#endif /* HAVE_SETEGID */
7986
Larry Hastings2f936352014-08-05 14:04:04 +10007987
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007988#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007989/*[clinic input]
7990os.setreuid
7991
7992 ruid: uid_t
7993 euid: uid_t
7994 /
7995
7996Set the current process's real and effective user ids.
7997[clinic start generated code]*/
7998
Larry Hastings2f936352014-08-05 14:04:04 +10007999static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008000os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
8001/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008002{
Victor Stinner8c62be82010-05-06 00:08:46 +00008003 if (setreuid(ruid, euid) < 0) {
8004 return posix_error();
8005 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02008006 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00008007 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008008}
8009#endif /* HAVE_SETREUID */
8010
Larry Hastings2f936352014-08-05 14:04:04 +10008011
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008012#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10008013/*[clinic input]
8014os.setregid
8015
8016 rgid: gid_t
8017 egid: gid_t
8018 /
8019
8020Set the current process's real and effective group ids.
8021[clinic start generated code]*/
8022
Larry Hastings2f936352014-08-05 14:04:04 +10008023static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008024os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
8025/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008026{
8027 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00008028 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008029 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00008030}
8031#endif /* HAVE_SETREGID */
8032
Larry Hastings2f936352014-08-05 14:04:04 +10008033
Guido van Rossumb6775db1994-08-01 11:34:53 +00008034#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10008035/*[clinic input]
8036os.setgid
8037 gid: gid_t
8038 /
8039
8040Set the current process's group id.
8041[clinic start generated code]*/
8042
Larry Hastings2f936352014-08-05 14:04:04 +10008043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008044os_setgid_impl(PyObject *module, gid_t gid)
8045/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008046{
Victor Stinner8c62be82010-05-06 00:08:46 +00008047 if (setgid(gid) < 0)
8048 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008049 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00008050}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008051#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00008052
Larry Hastings2f936352014-08-05 14:04:04 +10008053
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008054#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10008055/*[clinic input]
8056os.setgroups
8057
8058 groups: object
8059 /
8060
8061Set the groups of the current process to list.
8062[clinic start generated code]*/
8063
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008064static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008065os_setgroups(PyObject *module, PyObject *groups)
8066/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008067{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008068 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00008069 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00008070
Victor Stinner8c62be82010-05-06 00:08:46 +00008071 if (!PySequence_Check(groups)) {
8072 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
8073 return NULL;
8074 }
8075 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008076 if (len < 0) {
8077 return NULL;
8078 }
Victor Stinner8c62be82010-05-06 00:08:46 +00008079 if (len > MAX_GROUPS) {
8080 PyErr_SetString(PyExc_ValueError, "too many groups");
8081 return NULL;
8082 }
8083 for(i = 0; i < len; i++) {
8084 PyObject *elem;
8085 elem = PySequence_GetItem(groups, i);
8086 if (!elem)
8087 return NULL;
8088 if (!PyLong_Check(elem)) {
8089 PyErr_SetString(PyExc_TypeError,
8090 "groups must be integers");
8091 Py_DECREF(elem);
8092 return NULL;
8093 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02008094 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008095 Py_DECREF(elem);
8096 return NULL;
8097 }
8098 }
8099 Py_DECREF(elem);
8100 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008101
Victor Stinner8c62be82010-05-06 00:08:46 +00008102 if (setgroups(len, grouplist) < 0)
8103 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02008104 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00008105}
8106#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008107
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008108#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
8109static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02008110wait_helper(PyObject *module, pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008111{
Victor Stinner8c62be82010-05-06 00:08:46 +00008112 PyObject *result;
Eddie Elizondob3966632019-11-05 07:16:14 -08008113 PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008114
Victor Stinner8c62be82010-05-06 00:08:46 +00008115 if (pid == -1)
8116 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008117
Zackery Spytz682107c2019-09-09 09:48:32 -06008118 // If wait succeeded but no child was ready to report status, ru will not
8119 // have been populated.
8120 if (pid == 0) {
8121 memset(ru, 0, sizeof(*ru));
8122 }
8123
Eddie Elizondob3966632019-11-05 07:16:14 -08008124 PyObject *m = PyImport_ImportModuleNoBlock("resource");
8125 if (m == NULL)
8126 return NULL;
Victor Stinner1c2fa782020-05-10 11:05:29 +02008127 struct_rusage = PyObject_GetAttr(m, get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08008128 Py_DECREF(m);
8129 if (struct_rusage == NULL)
8130 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008131
Victor Stinner8c62be82010-05-06 00:08:46 +00008132 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
8133 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
Eddie Elizondoe4db1f02019-11-25 19:07:37 -08008134 Py_DECREF(struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00008135 if (!result)
8136 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008137
8138#ifndef doubletime
8139#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
8140#endif
8141
Victor Stinner8c62be82010-05-06 00:08:46 +00008142 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01008143 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00008144 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01008145 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008146#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00008147 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
8148 SET_INT(result, 2, ru->ru_maxrss);
8149 SET_INT(result, 3, ru->ru_ixrss);
8150 SET_INT(result, 4, ru->ru_idrss);
8151 SET_INT(result, 5, ru->ru_isrss);
8152 SET_INT(result, 6, ru->ru_minflt);
8153 SET_INT(result, 7, ru->ru_majflt);
8154 SET_INT(result, 8, ru->ru_nswap);
8155 SET_INT(result, 9, ru->ru_inblock);
8156 SET_INT(result, 10, ru->ru_oublock);
8157 SET_INT(result, 11, ru->ru_msgsnd);
8158 SET_INT(result, 12, ru->ru_msgrcv);
8159 SET_INT(result, 13, ru->ru_nsignals);
8160 SET_INT(result, 14, ru->ru_nvcsw);
8161 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008162#undef SET_INT
8163
Victor Stinner8c62be82010-05-06 00:08:46 +00008164 if (PyErr_Occurred()) {
8165 Py_DECREF(result);
8166 return NULL;
8167 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008168
Victor Stinner8c62be82010-05-06 00:08:46 +00008169 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008170}
8171#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
8172
Larry Hastings2f936352014-08-05 14:04:04 +10008173
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008174#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10008175/*[clinic input]
8176os.wait3
8177
8178 options: int
8179Wait for completion of a child process.
8180
8181Returns a tuple of information about the child process:
8182 (pid, status, rusage)
8183[clinic start generated code]*/
8184
Larry Hastings2f936352014-08-05 14:04:04 +10008185static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008186os_wait3_impl(PyObject *module, int options)
8187/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008188{
Victor Stinner8c62be82010-05-06 00:08:46 +00008189 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008190 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008191 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008192 WAIT_TYPE status;
8193 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008194
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008195 do {
8196 Py_BEGIN_ALLOW_THREADS
8197 pid = wait3(&status, options, &ru);
8198 Py_END_ALLOW_THREADS
8199 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8200 if (pid < 0)
8201 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008202
Victor Stinner1c2fa782020-05-10 11:05:29 +02008203 return wait_helper(module, pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008204}
8205#endif /* HAVE_WAIT3 */
8206
Larry Hastings2f936352014-08-05 14:04:04 +10008207
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008208#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10008209/*[clinic input]
8210
8211os.wait4
8212
8213 pid: pid_t
8214 options: int
8215
8216Wait for completion of a specific child process.
8217
8218Returns a tuple of information about the child process:
8219 (pid, status, rusage)
8220[clinic start generated code]*/
8221
Larry Hastings2f936352014-08-05 14:04:04 +10008222static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008223os_wait4_impl(PyObject *module, pid_t pid, int options)
8224/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008225{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008226 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00008227 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008228 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008229 WAIT_TYPE status;
8230 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008231
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008232 do {
8233 Py_BEGIN_ALLOW_THREADS
8234 res = wait4(pid, &status, options, &ru);
8235 Py_END_ALLOW_THREADS
8236 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8237 if (res < 0)
8238 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008239
Victor Stinner1c2fa782020-05-10 11:05:29 +02008240 return wait_helper(module, res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008241}
8242#endif /* HAVE_WAIT4 */
8243
Larry Hastings2f936352014-08-05 14:04:04 +10008244
Ross Lagerwall7807c352011-03-17 20:20:30 +02008245#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10008246/*[clinic input]
8247os.waitid
8248
8249 idtype: idtype_t
8250 Must be one of be P_PID, P_PGID or P_ALL.
8251 id: id_t
8252 The id to wait on.
8253 options: int
8254 Constructed from the ORing of one or more of WEXITED, WSTOPPED
8255 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
8256 /
8257
8258Returns the result of waiting for a process or processes.
8259
8260Returns either waitid_result or None if WNOHANG is specified and there are
8261no children in a waitable state.
8262[clinic start generated code]*/
8263
Larry Hastings2f936352014-08-05 14:04:04 +10008264static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008265os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
8266/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008267{
8268 PyObject *result;
8269 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008270 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008271 siginfo_t si;
8272 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008273
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008274 do {
8275 Py_BEGIN_ALLOW_THREADS
8276 res = waitid(idtype, id, &si, options);
8277 Py_END_ALLOW_THREADS
8278 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8279 if (res < 0)
8280 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008281
8282 if (si.si_pid == 0)
8283 Py_RETURN_NONE;
8284
Hai Shif707d942020-03-16 21:15:01 +08008285 PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08008286 result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008287 if (!result)
8288 return NULL;
8289
8290 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02008291 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02008292 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
8293 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
8294 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
8295 if (PyErr_Occurred()) {
8296 Py_DECREF(result);
8297 return NULL;
8298 }
8299
8300 return result;
8301}
Larry Hastings2f936352014-08-05 14:04:04 +10008302#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008303
Larry Hastings2f936352014-08-05 14:04:04 +10008304
8305#if defined(HAVE_WAITPID)
8306/*[clinic input]
8307os.waitpid
8308 pid: pid_t
8309 options: int
8310 /
8311
8312Wait for completion of a given child process.
8313
8314Returns a tuple of information regarding the child process:
8315 (pid, status)
8316
8317The options argument is ignored on Windows.
8318[clinic start generated code]*/
8319
Larry Hastings2f936352014-08-05 14:04:04 +10008320static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008321os_waitpid_impl(PyObject *module, pid_t pid, int options)
8322/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008323{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008324 pid_t res;
8325 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008326 WAIT_TYPE status;
8327 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00008328
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008329 do {
8330 Py_BEGIN_ALLOW_THREADS
8331 res = waitpid(pid, &status, options);
8332 Py_END_ALLOW_THREADS
8333 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8334 if (res < 0)
8335 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008336
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008337 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00008338}
Tim Petersab034fa2002-02-01 11:27:43 +00008339#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00008340/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10008341/*[clinic input]
8342os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07008343 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10008344 options: int
8345 /
8346
8347Wait for completion of a given process.
8348
8349Returns a tuple of information regarding the process:
8350 (pid, status << 8)
8351
8352The options argument is ignored on Windows.
8353[clinic start generated code]*/
8354
Larry Hastings2f936352014-08-05 14:04:04 +10008355static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07008356os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07008357/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008358{
8359 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07008360 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008361 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008362
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008363 do {
8364 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08008365 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008366 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08008367 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008368 Py_END_ALLOW_THREADS
8369 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008370 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008371 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008372
Victor Stinner9bee32b2020-04-22 16:30:35 +02008373 unsigned long long ustatus = (unsigned int)status;
8374
Victor Stinner8c62be82010-05-06 00:08:46 +00008375 /* shift the status left a byte so this is more like the POSIX waitpid */
Victor Stinner9bee32b2020-04-22 16:30:35 +02008376 return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00008377}
Larry Hastings2f936352014-08-05 14:04:04 +10008378#endif
8379
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008380
Guido van Rossumad0ee831995-03-01 10:34:45 +00008381#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10008382/*[clinic input]
8383os.wait
8384
8385Wait for completion of a child process.
8386
8387Returns a tuple of information about the child process:
8388 (pid, status)
8389[clinic start generated code]*/
8390
Larry Hastings2f936352014-08-05 14:04:04 +10008391static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008392os_wait_impl(PyObject *module)
8393/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00008394{
Victor Stinner8c62be82010-05-06 00:08:46 +00008395 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008396 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00008397 WAIT_TYPE status;
8398 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00008399
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008400 do {
8401 Py_BEGIN_ALLOW_THREADS
8402 pid = wait(&status);
8403 Py_END_ALLOW_THREADS
8404 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8405 if (pid < 0)
8406 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008407
Victor Stinner8c62be82010-05-06 00:08:46 +00008408 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00008409}
Larry Hastings2f936352014-08-05 14:04:04 +10008410#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00008411
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08008412#if defined(__linux__) && defined(__NR_pidfd_open)
8413/*[clinic input]
8414os.pidfd_open
8415 pid: pid_t
8416 flags: unsigned_int = 0
8417
8418Return a file descriptor referring to the process *pid*.
8419
8420The descriptor can be used to perform process management without races and
8421signals.
8422[clinic start generated code]*/
8423
8424static PyObject *
8425os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
8426/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
8427{
8428 int fd = syscall(__NR_pidfd_open, pid, flags);
8429 if (fd < 0) {
8430 return posix_error();
8431 }
8432 return PyLong_FromLong(fd);
8433}
8434#endif
8435
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008436
Larry Hastings9cf065c2012-06-22 16:30:09 -07008437#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008438/*[clinic input]
8439os.readlink
8440
8441 path: path_t
8442 *
8443 dir_fd: dir_fd(requires='readlinkat') = None
8444
8445Return a string representing the path to which the symbolic link points.
8446
8447If dir_fd is not None, it should be a file descriptor open to a directory,
8448and path should be relative; path will then be relative to that directory.
8449
8450dir_fd may not be implemented on your platform. If it is unavailable,
8451using it will raise a NotImplementedError.
8452[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008453
Barry Warsaw53699e91996-12-10 23:23:01 +00008454static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008455os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
8456/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008457{
Berker Peksage0b5b202018-08-15 13:03:41 +03008458#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02008459 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07008460 ssize_t length;
Ronald Oussoren41761932020-11-08 10:05:27 +01008461#ifdef HAVE_READLINKAT
8462 int readlinkat_unavailable = 0;
8463#endif
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008464
8465 Py_BEGIN_ALLOW_THREADS
8466#ifdef HAVE_READLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +01008467 if (dir_fd != DEFAULT_DIR_FD) {
8468 if (HAVE_READLINKAT_RUNTIME) {
8469 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
8470 } else {
8471 readlinkat_unavailable = 1;
8472 }
8473 } else
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008474#endif
8475 length = readlink(path->narrow, buffer, MAXPATHLEN);
8476 Py_END_ALLOW_THREADS
8477
Ronald Oussoren41761932020-11-08 10:05:27 +01008478#ifdef HAVE_READLINKAT
8479 if (readlinkat_unavailable) {
8480 argument_unavailable_error(NULL, "dir_fd");
8481 return NULL;
8482 }
8483#endif
8484
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008485 if (length < 0) {
8486 return path_error(path);
8487 }
8488 buffer[length] = '\0';
8489
8490 if (PyUnicode_Check(path->object))
8491 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
8492 else
8493 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03008494#elif defined(MS_WINDOWS)
8495 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008496 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03008497 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03008498 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
8499 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07008500 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00008501
Larry Hastings2f936352014-08-05 14:04:04 +10008502 /* First get a handle to the reparse point */
8503 Py_BEGIN_ALLOW_THREADS
8504 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008505 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10008506 0,
8507 0,
8508 0,
8509 OPEN_EXISTING,
8510 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
8511 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008512 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
8513 /* New call DeviceIoControl to read the reparse point */
8514 io_result = DeviceIoControl(
8515 reparse_point_handle,
8516 FSCTL_GET_REPARSE_POINT,
8517 0, 0, /* in buffer */
8518 target_buffer, sizeof(target_buffer),
8519 &n_bytes_returned,
8520 0 /* we're not using OVERLAPPED_IO */
8521 );
8522 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03008523 }
Larry Hastings2f936352014-08-05 14:04:04 +10008524 Py_END_ALLOW_THREADS
8525
Berker Peksage0b5b202018-08-15 13:03:41 +03008526 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008527 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03008528 }
Larry Hastings2f936352014-08-05 14:04:04 +10008529
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008530 wchar_t *name = NULL;
8531 Py_ssize_t nameLen = 0;
8532 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10008533 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008534 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
8535 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
8536 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10008537 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008538 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
8539 {
8540 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
8541 rdb->MountPointReparseBuffer.SubstituteNameOffset);
8542 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
8543 }
8544 else
8545 {
8546 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
8547 }
8548 if (name) {
8549 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
8550 /* Our buffer is mutable, so this is okay */
8551 name[1] = L'\\';
8552 }
8553 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07008554 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008555 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
8556 }
Berker Peksage0b5b202018-08-15 13:03:41 +03008557 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008558 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03008559#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008560}
Berker Peksage0b5b202018-08-15 13:03:41 +03008561#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008562
Larry Hastings9cf065c2012-06-22 16:30:09 -07008563#if defined(MS_WINDOWS)
8564
Steve Dower6921e732018-03-05 14:26:08 -08008565/* Remove the last portion of the path - return 0 on success */
8566static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008567_dirnameW(WCHAR *path)
8568{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008569 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08008570 size_t length = wcsnlen_s(path, MAX_PATH);
8571 if (length == MAX_PATH) {
8572 return -1;
8573 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008574
8575 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08008576 for(ptr = path + length; ptr != path; ptr--) {
8577 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04008578 break;
Steve Dower6921e732018-03-05 14:26:08 -08008579 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008580 }
8581 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08008582 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008583}
8584
Minmin Gong7f21c9a2020-05-18 09:17:19 -07008585#endif
8586
8587#ifdef HAVE_SYMLINK
8588
8589#if defined(MS_WINDOWS)
8590
Victor Stinner31b3b922013-06-05 01:49:17 +02008591/* Is this path absolute? */
8592static int
8593_is_absW(const WCHAR *path)
8594{
Steve Dower6921e732018-03-05 14:26:08 -08008595 return path[0] == L'\\' || path[0] == L'/' ||
8596 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04008597}
8598
Steve Dower6921e732018-03-05 14:26:08 -08008599/* join root and rest with a backslash - return 0 on success */
8600static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008601_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8602{
Victor Stinner31b3b922013-06-05 01:49:17 +02008603 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08008604 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008605 }
8606
Steve Dower6921e732018-03-05 14:26:08 -08008607 if (wcscpy_s(dest_path, MAX_PATH, root)) {
8608 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008609 }
Steve Dower6921e732018-03-05 14:26:08 -08008610
8611 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8612 return -1;
8613 }
8614
8615 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008616}
8617
Victor Stinner31b3b922013-06-05 01:49:17 +02008618/* Return True if the path at src relative to dest is a directory */
8619static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008620_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04008621{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008622 WIN32_FILE_ATTRIBUTE_DATA src_info;
8623 WCHAR dest_parent[MAX_PATH];
8624 WCHAR src_resolved[MAX_PATH] = L"";
8625
8626 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08008627 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8628 _dirnameW(dest_parent)) {
8629 return 0;
8630 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008631 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08008632 if (_joinW(src_resolved, dest_parent, src)) {
8633 return 0;
8634 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008635 return (
8636 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8637 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8638 );
8639}
Larry Hastings9cf065c2012-06-22 16:30:09 -07008640#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008641
Larry Hastings2f936352014-08-05 14:04:04 +10008642
8643/*[clinic input]
8644os.symlink
8645 src: path_t
8646 dst: path_t
8647 target_is_directory: bool = False
8648 *
8649 dir_fd: dir_fd(requires='symlinkat')=None
8650
8651# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8652
8653Create a symbolic link pointing to src named dst.
8654
8655target_is_directory is required on Windows if the target is to be
8656 interpreted as a directory. (On Windows, symlink requires
8657 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8658 target_is_directory is ignored on non-Windows platforms.
8659
8660If dir_fd is not None, it should be a file descriptor open to a directory,
8661 and path should be relative; path will then be relative to that directory.
8662dir_fd may not be implemented on your platform.
8663 If it is unavailable, using it will raise a NotImplementedError.
8664
8665[clinic start generated code]*/
8666
Larry Hastings2f936352014-08-05 14:04:04 +10008667static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008668os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008669 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008670/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008671{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008672#ifdef MS_WINDOWS
8673 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008674 DWORD flags = 0;
8675
8676 /* Assumed true, set to false if detected to not be available. */
8677 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008678#else
8679 int result;
Ronald Oussoren41761932020-11-08 10:05:27 +01008680#ifdef HAVE_SYMLINKAT
8681 int symlinkat_unavailable = 0;
8682#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07008683#endif
8684
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008685 if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
8686 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
8687 return NULL;
8688 }
8689
Larry Hastings9cf065c2012-06-22 16:30:09 -07008690#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008691
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008692 if (windows_has_symlink_unprivileged_flag) {
8693 /* Allow non-admin symlinks if system allows it. */
8694 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8695 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008696
Larry Hastings9cf065c2012-06-22 16:30:09 -07008697 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008698 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008699 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8700 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8701 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8702 }
8703
8704 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008705 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008706 Py_END_ALLOW_THREADS
8707
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008708 if (windows_has_symlink_unprivileged_flag && !result &&
8709 ERROR_INVALID_PARAMETER == GetLastError()) {
8710
8711 Py_BEGIN_ALLOW_THREADS
8712 _Py_BEGIN_SUPPRESS_IPH
8713 /* This error might be caused by
8714 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8715 Try again, and update windows_has_symlink_unprivileged_flag if we
8716 are successful this time.
8717
8718 NOTE: There is a risk of a race condition here if there are other
8719 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8720 another process (or thread) changes that condition in between our
8721 calls to CreateSymbolicLink.
8722 */
8723 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8724 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8725 _Py_END_SUPPRESS_IPH
8726 Py_END_ALLOW_THREADS
8727
8728 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8729 windows_has_symlink_unprivileged_flag = FALSE;
8730 }
8731 }
8732
Larry Hastings2f936352014-08-05 14:04:04 +10008733 if (!result)
8734 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008735
8736#else
8737
Steve Dower6921e732018-03-05 14:26:08 -08008738 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8739 PyErr_SetString(PyExc_ValueError,
8740 "symlink: src and dst must be the same type");
8741 return NULL;
8742 }
8743
Larry Hastings9cf065c2012-06-22 16:30:09 -07008744 Py_BEGIN_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +01008745#ifdef HAVE_SYMLINKAT
8746 if (dir_fd != DEFAULT_DIR_FD) {
8747 if (HAVE_SYMLINKAT_RUNTIME) {
8748 result = symlinkat(src->narrow, dir_fd, dst->narrow);
8749 } else {
8750 symlinkat_unavailable = 1;
8751 }
8752 } else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008753#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008754 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008755 Py_END_ALLOW_THREADS
8756
Ronald Oussoren41761932020-11-08 10:05:27 +01008757#ifdef HAVE_SYMLINKAT
8758 if (symlinkat_unavailable) {
8759 argument_unavailable_error(NULL, "dir_fd");
8760 return NULL;
8761 }
8762#endif
8763
Larry Hastings2f936352014-08-05 14:04:04 +10008764 if (result)
8765 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008766#endif
8767
Larry Hastings2f936352014-08-05 14:04:04 +10008768 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008769}
8770#endif /* HAVE_SYMLINK */
8771
Larry Hastings9cf065c2012-06-22 16:30:09 -07008772
Brian Curtind40e6f72010-07-08 21:39:08 +00008773
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008774
Larry Hastings605a62d2012-06-24 04:33:36 -07008775static PyStructSequence_Field times_result_fields[] = {
8776 {"user", "user time"},
8777 {"system", "system time"},
8778 {"children_user", "user time of children"},
8779 {"children_system", "system time of children"},
8780 {"elapsed", "elapsed time since an arbitrary point in the past"},
8781 {NULL}
8782};
8783
8784PyDoc_STRVAR(times_result__doc__,
8785"times_result: Result from os.times().\n\n\
8786This object may be accessed either as a tuple of\n\
8787 (user, system, children_user, children_system, elapsed),\n\
8788or via the attributes user, system, children_user, children_system,\n\
8789and elapsed.\n\
8790\n\
8791See os.times for more information.");
8792
8793static PyStructSequence_Desc times_result_desc = {
8794 "times_result", /* name */
8795 times_result__doc__, /* doc */
8796 times_result_fields,
8797 5
8798};
8799
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008800#ifdef MS_WINDOWS
8801#define HAVE_TIMES /* mandatory, for the method table */
8802#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008803
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008804#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008805
8806static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +02008807build_times_result(PyObject *module, double user, double system,
Larry Hastings605a62d2012-06-24 04:33:36 -07008808 double children_user, double children_system,
8809 double elapsed)
8810{
Victor Stinner1c2fa782020-05-10 11:05:29 +02008811 PyObject *TimesResultType = get_posix_state(module)->TimesResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08008812 PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008813 if (value == NULL)
8814 return NULL;
8815
8816#define SET(i, field) \
8817 { \
8818 PyObject *o = PyFloat_FromDouble(field); \
8819 if (!o) { \
8820 Py_DECREF(value); \
8821 return NULL; \
8822 } \
8823 PyStructSequence_SET_ITEM(value, i, o); \
8824 } \
8825
8826 SET(0, user);
8827 SET(1, system);
8828 SET(2, children_user);
8829 SET(3, children_system);
8830 SET(4, elapsed);
8831
8832#undef SET
8833
8834 return value;
8835}
8836
Larry Hastings605a62d2012-06-24 04:33:36 -07008837
Larry Hastings2f936352014-08-05 14:04:04 +10008838#ifndef MS_WINDOWS
8839#define NEED_TICKS_PER_SECOND
8840static long ticks_per_second = -1;
8841#endif /* MS_WINDOWS */
8842
8843/*[clinic input]
8844os.times
8845
8846Return a collection containing process timing information.
8847
8848The object returned behaves like a named tuple with these fields:
8849 (utime, stime, cutime, cstime, elapsed_time)
8850All fields are floating point numbers.
8851[clinic start generated code]*/
8852
Larry Hastings2f936352014-08-05 14:04:04 +10008853static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008854os_times_impl(PyObject *module)
8855/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008856#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008857{
Victor Stinner8c62be82010-05-06 00:08:46 +00008858 FILETIME create, exit, kernel, user;
8859 HANDLE hProc;
8860 hProc = GetCurrentProcess();
8861 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8862 /* The fields of a FILETIME structure are the hi and lo part
8863 of a 64-bit value expressed in 100 nanosecond units.
8864 1e7 is one second in such units; 1e-7 the inverse.
8865 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8866 */
Victor Stinner1c2fa782020-05-10 11:05:29 +02008867 return build_times_result(module,
Victor Stinner8c62be82010-05-06 00:08:46 +00008868 (double)(user.dwHighDateTime*429.4967296 +
8869 user.dwLowDateTime*1e-7),
8870 (double)(kernel.dwHighDateTime*429.4967296 +
8871 kernel.dwLowDateTime*1e-7),
8872 (double)0,
8873 (double)0,
8874 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008875}
Larry Hastings2f936352014-08-05 14:04:04 +10008876#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008877{
Larry Hastings2f936352014-08-05 14:04:04 +10008878
8879
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008880 struct tms t;
8881 clock_t c;
8882 errno = 0;
8883 c = times(&t);
8884 if (c == (clock_t) -1)
8885 return posix_error();
Victor Stinner1c2fa782020-05-10 11:05:29 +02008886 return build_times_result(module,
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008887 (double)t.tms_utime / ticks_per_second,
8888 (double)t.tms_stime / ticks_per_second,
8889 (double)t.tms_cutime / ticks_per_second,
8890 (double)t.tms_cstime / ticks_per_second,
8891 (double)c / ticks_per_second);
8892}
Larry Hastings2f936352014-08-05 14:04:04 +10008893#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008894#endif /* HAVE_TIMES */
8895
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008896
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008897#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008898/*[clinic input]
8899os.getsid
8900
8901 pid: pid_t
8902 /
8903
8904Call the system call getsid(pid) and return the result.
8905[clinic start generated code]*/
8906
Larry Hastings2f936352014-08-05 14:04:04 +10008907static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008908os_getsid_impl(PyObject *module, pid_t pid)
8909/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008910{
Victor Stinner8c62be82010-05-06 00:08:46 +00008911 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008912 sid = getsid(pid);
8913 if (sid < 0)
8914 return posix_error();
8915 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008916}
8917#endif /* HAVE_GETSID */
8918
8919
Guido van Rossumb6775db1994-08-01 11:34:53 +00008920#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008921/*[clinic input]
8922os.setsid
8923
8924Call the system call setsid().
8925[clinic start generated code]*/
8926
Larry Hastings2f936352014-08-05 14:04:04 +10008927static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008928os_setsid_impl(PyObject *module)
8929/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008930{
Victor Stinner8c62be82010-05-06 00:08:46 +00008931 if (setsid() < 0)
8932 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008933 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008934}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008935#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008936
Larry Hastings2f936352014-08-05 14:04:04 +10008937
Guido van Rossumb6775db1994-08-01 11:34:53 +00008938#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008939/*[clinic input]
8940os.setpgid
8941
8942 pid: pid_t
8943 pgrp: pid_t
8944 /
8945
8946Call the system call setpgid(pid, pgrp).
8947[clinic start generated code]*/
8948
Larry Hastings2f936352014-08-05 14:04:04 +10008949static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008950os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8951/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008952{
Victor Stinner8c62be82010-05-06 00:08:46 +00008953 if (setpgid(pid, pgrp) < 0)
8954 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008955 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008956}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008957#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008958
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008959
Guido van Rossumb6775db1994-08-01 11:34:53 +00008960#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008961/*[clinic input]
8962os.tcgetpgrp
8963
8964 fd: int
8965 /
8966
8967Return the process group associated with the terminal specified by fd.
8968[clinic start generated code]*/
8969
Larry Hastings2f936352014-08-05 14:04:04 +10008970static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008971os_tcgetpgrp_impl(PyObject *module, int fd)
8972/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008973{
8974 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008975 if (pgid < 0)
8976 return posix_error();
8977 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008978}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008979#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008980
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008981
Guido van Rossumb6775db1994-08-01 11:34:53 +00008982#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008983/*[clinic input]
8984os.tcsetpgrp
8985
8986 fd: int
8987 pgid: pid_t
8988 /
8989
8990Set the process group associated with the terminal specified by fd.
8991[clinic start generated code]*/
8992
Larry Hastings2f936352014-08-05 14:04:04 +10008993static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008994os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8995/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008996{
Victor Stinner8c62be82010-05-06 00:08:46 +00008997 if (tcsetpgrp(fd, pgid) < 0)
8998 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008999 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00009000}
Guido van Rossumb6775db1994-08-01 11:34:53 +00009001#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00009002
Guido van Rossum687dd131993-05-17 08:34:16 +00009003/* Functions acting on file descriptors */
9004
Victor Stinnerdaf45552013-08-28 00:53:59 +02009005#ifdef O_CLOEXEC
9006extern int _Py_open_cloexec_works;
9007#endif
9008
Larry Hastings2f936352014-08-05 14:04:04 +10009009
9010/*[clinic input]
9011os.open -> int
9012 path: path_t
9013 flags: int
9014 mode: int = 0o777
9015 *
9016 dir_fd: dir_fd(requires='openat') = None
9017
9018# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
9019
9020Open a file for low level IO. Returns a file descriptor (integer).
9021
9022If dir_fd is not None, it should be a file descriptor open to a directory,
9023 and path should be relative; path will then be relative to that directory.
9024dir_fd may not be implemented on your platform.
9025 If it is unavailable, using it will raise a NotImplementedError.
9026[clinic start generated code]*/
9027
Larry Hastings2f936352014-08-05 14:04:04 +10009028static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009029os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
9030/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009031{
9032 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009033 int async_err = 0;
Ronald Oussoren41761932020-11-08 10:05:27 +01009034#ifdef HAVE_OPENAT
9035 int openat_unavailable = 0;
9036#endif
Larry Hastings2f936352014-08-05 14:04:04 +10009037
Victor Stinnerdaf45552013-08-28 00:53:59 +02009038#ifdef O_CLOEXEC
9039 int *atomic_flag_works = &_Py_open_cloexec_works;
9040#elif !defined(MS_WINDOWS)
9041 int *atomic_flag_works = NULL;
9042#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00009043
Victor Stinnerdaf45552013-08-28 00:53:59 +02009044#ifdef MS_WINDOWS
9045 flags |= O_NOINHERIT;
9046#elif defined(O_CLOEXEC)
9047 flags |= O_CLOEXEC;
9048#endif
9049
Steve Dowerb82e17e2019-05-23 08:45:22 -07009050 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
9051 return -1;
9052 }
9053
Steve Dower8fc89802015-04-12 00:26:27 -04009054 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009055 do {
9056 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07009057#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07009058 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07009059#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07009060#ifdef HAVE_OPENAT
Ronald Oussoren41761932020-11-08 10:05:27 +01009061 if (dir_fd != DEFAULT_DIR_FD) {
9062 if (HAVE_OPENAT_RUNTIME) {
9063 fd = openat(dir_fd, path->narrow, flags, mode);
9064
9065 } else {
9066 openat_unavailable = 1;
9067 fd = -1;
9068 }
9069 } else
Steve Dower6230aaf2016-09-09 09:03:15 -07009070#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009071 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07009072#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009073 Py_END_ALLOW_THREADS
9074 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009075 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00009076
Ronald Oussoren41761932020-11-08 10:05:27 +01009077#ifdef HAVE_OPENAT
9078 if (openat_unavailable) {
9079 argument_unavailable_error(NULL, "dir_fd");
9080 return -1;
9081 }
9082#endif
9083
Victor Stinnerd3ffd322015-09-15 10:11:03 +02009084 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009085 if (!async_err)
9086 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10009087 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07009088 }
9089
Victor Stinnerdaf45552013-08-28 00:53:59 +02009090#ifndef MS_WINDOWS
9091 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
9092 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10009093 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009094 }
9095#endif
9096
Larry Hastings2f936352014-08-05 14:04:04 +10009097 return fd;
9098}
9099
9100
9101/*[clinic input]
9102os.close
9103
9104 fd: int
9105
9106Close a file descriptor.
9107[clinic start generated code]*/
9108
Barry Warsaw53699e91996-12-10 23:23:01 +00009109static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009110os_close_impl(PyObject *module, int fd)
9111/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009112{
Larry Hastings2f936352014-08-05 14:04:04 +10009113 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009114 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
9115 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
9116 * for more details.
9117 */
Victor Stinner8c62be82010-05-06 00:08:46 +00009118 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009119 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009120 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04009121 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009122 Py_END_ALLOW_THREADS
9123 if (res < 0)
9124 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009125 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00009126}
9127
Larry Hastings2f936352014-08-05 14:04:04 +10009128/*[clinic input]
9129os.closerange
9130
9131 fd_low: int
9132 fd_high: int
9133 /
9134
9135Closes all file descriptors in [fd_low, fd_high), ignoring errors.
9136[clinic start generated code]*/
9137
Larry Hastings2f936352014-08-05 14:04:04 +10009138static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009139os_closerange_impl(PyObject *module, int fd_low, int fd_high)
9140/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009141{
Victor Stinner8c62be82010-05-06 00:08:46 +00009142 Py_BEGIN_ALLOW_THREADS
Kyle Evansc230fde2020-10-11 13:54:11 -05009143 _Py_closerange(fd_low, fd_high - 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00009144 Py_END_ALLOW_THREADS
9145 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00009146}
9147
9148
Larry Hastings2f936352014-08-05 14:04:04 +10009149/*[clinic input]
9150os.dup -> int
9151
9152 fd: int
9153 /
9154
9155Return a duplicate of a file descriptor.
9156[clinic start generated code]*/
9157
Larry Hastings2f936352014-08-05 14:04:04 +10009158static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009159os_dup_impl(PyObject *module, int fd)
9160/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009161{
9162 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00009163}
9164
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009165
Larry Hastings2f936352014-08-05 14:04:04 +10009166/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009167os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10009168 fd: int
9169 fd2: int
9170 inheritable: bool=True
9171
9172Duplicate file descriptor.
9173[clinic start generated code]*/
9174
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009175static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009176os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009177/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009178{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01009179 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009180#if defined(HAVE_DUP3) && \
9181 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
9182 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03009183 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009184#endif
9185
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009186 if (fd < 0 || fd2 < 0) {
9187 posix_error();
9188 return -1;
9189 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009190
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009191 /* dup2() can fail with EINTR if the target FD is already open, because it
9192 * then has to be closed. See os_close_impl() for why we don't handle EINTR
9193 * upon close(), and therefore below.
9194 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02009195#ifdef MS_WINDOWS
9196 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009197 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009198 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04009199 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009200 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009201 if (res < 0) {
9202 posix_error();
9203 return -1;
9204 }
9205 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02009206
9207 /* Character files like console cannot be make non-inheritable */
9208 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
9209 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009210 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009211 }
9212
9213#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
9214 Py_BEGIN_ALLOW_THREADS
9215 if (!inheritable)
9216 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
9217 else
9218 res = dup2(fd, fd2);
9219 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009220 if (res < 0) {
9221 posix_error();
9222 return -1;
9223 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009224
9225#else
9226
9227#ifdef HAVE_DUP3
9228 if (!inheritable && dup3_works != 0) {
9229 Py_BEGIN_ALLOW_THREADS
9230 res = dup3(fd, fd2, O_CLOEXEC);
9231 Py_END_ALLOW_THREADS
9232 if (res < 0) {
9233 if (dup3_works == -1)
9234 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009235 if (dup3_works) {
9236 posix_error();
9237 return -1;
9238 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009239 }
9240 }
9241
9242 if (inheritable || dup3_works == 0)
9243 {
9244#endif
9245 Py_BEGIN_ALLOW_THREADS
9246 res = dup2(fd, fd2);
9247 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009248 if (res < 0) {
9249 posix_error();
9250 return -1;
9251 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02009252
9253 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
9254 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009255 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009256 }
9257#ifdef HAVE_DUP3
9258 }
9259#endif
9260
9261#endif
9262
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08009263 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00009264}
9265
Larry Hastings2f936352014-08-05 14:04:04 +10009266
Ross Lagerwall7807c352011-03-17 20:20:30 +02009267#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10009268/*[clinic input]
9269os.lockf
9270
9271 fd: int
9272 An open file descriptor.
9273 command: int
9274 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
9275 length: Py_off_t
9276 The number of bytes to lock, starting at the current position.
9277 /
9278
9279Apply, test or remove a POSIX lock on an open file descriptor.
9280
9281[clinic start generated code]*/
9282
Larry Hastings2f936352014-08-05 14:04:04 +10009283static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009284os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
9285/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009286{
9287 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009288
Saiyang Gou7514f4f2020-02-12 23:47:42 -08009289 if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
9290 return NULL;
9291 }
9292
Ross Lagerwall7807c352011-03-17 20:20:30 +02009293 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009294 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009295 Py_END_ALLOW_THREADS
9296
9297 if (res < 0)
9298 return posix_error();
9299
9300 Py_RETURN_NONE;
9301}
Larry Hastings2f936352014-08-05 14:04:04 +10009302#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009303
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009304
Larry Hastings2f936352014-08-05 14:04:04 +10009305/*[clinic input]
9306os.lseek -> Py_off_t
9307
9308 fd: int
9309 position: Py_off_t
9310 how: int
9311 /
9312
9313Set the position of a file descriptor. Return the new position.
9314
9315Return the new cursor position in number of bytes
9316relative to the beginning of the file.
9317[clinic start generated code]*/
9318
Larry Hastings2f936352014-08-05 14:04:04 +10009319static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009320os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
9321/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009322{
9323 Py_off_t result;
9324
Guido van Rossum687dd131993-05-17 08:34:16 +00009325#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00009326 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
9327 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10009328 case 0: how = SEEK_SET; break;
9329 case 1: how = SEEK_CUR; break;
9330 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00009331 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00009332#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00009333
Victor Stinner8c62be82010-05-06 00:08:46 +00009334 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009335 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02009336#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10009337 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00009338#else
Larry Hastings2f936352014-08-05 14:04:04 +10009339 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00009340#endif
Steve Dower8fc89802015-04-12 00:26:27 -04009341 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00009342 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009343 if (result < 0)
9344 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00009345
Larry Hastings2f936352014-08-05 14:04:04 +10009346 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00009347}
9348
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009349
Larry Hastings2f936352014-08-05 14:04:04 +10009350/*[clinic input]
9351os.read
9352 fd: int
9353 length: Py_ssize_t
9354 /
9355
9356Read from a file descriptor. Returns a bytes object.
9357[clinic start generated code]*/
9358
Larry Hastings2f936352014-08-05 14:04:04 +10009359static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009360os_read_impl(PyObject *module, int fd, Py_ssize_t length)
9361/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009362{
Victor Stinner8c62be82010-05-06 00:08:46 +00009363 Py_ssize_t n;
9364 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10009365
9366 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009367 errno = EINVAL;
9368 return posix_error();
9369 }
Larry Hastings2f936352014-08-05 14:04:04 +10009370
Victor Stinner9a0d7a72018-11-22 15:03:40 +01009371 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10009372
9373 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00009374 if (buffer == NULL)
9375 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009376
Victor Stinner66aab0c2015-03-19 22:53:20 +01009377 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
9378 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00009379 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01009380 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00009381 }
Larry Hastings2f936352014-08-05 14:04:04 +10009382
9383 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00009384 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10009385
Victor Stinner8c62be82010-05-06 00:08:46 +00009386 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00009387}
9388
Ross Lagerwall7807c352011-03-17 20:20:30 +02009389#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009390 || defined(__APPLE__))) \
9391 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
9392 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9393static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009394iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009395{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009396 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009397
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009398 *iov = PyMem_New(struct iovec, cnt);
9399 if (*iov == NULL) {
9400 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01009401 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009402 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009403
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009404 *buf = PyMem_New(Py_buffer, cnt);
9405 if (*buf == NULL) {
9406 PyMem_Del(*iov);
9407 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01009408 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009409 }
9410
9411 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009412 PyObject *item = PySequence_GetItem(seq, i);
9413 if (item == NULL)
9414 goto fail;
9415 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
9416 Py_DECREF(item);
9417 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009418 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009419 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009420 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009421 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009422 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009423 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009424
9425fail:
9426 PyMem_Del(*iov);
9427 for (j = 0; j < i; j++) {
9428 PyBuffer_Release(&(*buf)[j]);
9429 }
9430 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01009431 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009432}
9433
9434static void
9435iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
9436{
9437 int i;
9438 PyMem_Del(iov);
9439 for (i = 0; i < cnt; i++) {
9440 PyBuffer_Release(&buf[i]);
9441 }
9442 PyMem_Del(buf);
9443}
9444#endif
9445
Larry Hastings2f936352014-08-05 14:04:04 +10009446
Ross Lagerwall7807c352011-03-17 20:20:30 +02009447#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10009448/*[clinic input]
9449os.readv -> Py_ssize_t
9450
9451 fd: int
9452 buffers: object
9453 /
9454
9455Read from a file descriptor fd into an iterable of buffers.
9456
9457The buffers should be mutable buffers accepting bytes.
9458readv will transfer data into each buffer until it is full
9459and then move on to the next buffer in the sequence to hold
9460the rest of the data.
9461
9462readv returns the total number of bytes read,
9463which may be less than the total capacity of all the buffers.
9464[clinic start generated code]*/
9465
Larry Hastings2f936352014-08-05 14:04:04 +10009466static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009467os_readv_impl(PyObject *module, int fd, PyObject *buffers)
9468/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009469{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009470 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009471 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009472 struct iovec *iov;
9473 Py_buffer *buf;
9474
Larry Hastings2f936352014-08-05 14:04:04 +10009475 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009476 PyErr_SetString(PyExc_TypeError,
9477 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009478 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009479 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02009480
Larry Hastings2f936352014-08-05 14:04:04 +10009481 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009482 if (cnt < 0)
9483 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10009484
9485 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
9486 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009487
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009488 do {
9489 Py_BEGIN_ALLOW_THREADS
9490 n = readv(fd, iov, cnt);
9491 Py_END_ALLOW_THREADS
9492 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009493
9494 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10009495 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009496 if (!async_err)
9497 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009498 return -1;
9499 }
Victor Stinner57ddf782014-01-08 15:21:28 +01009500
Larry Hastings2f936352014-08-05 14:04:04 +10009501 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009502}
Larry Hastings2f936352014-08-05 14:04:04 +10009503#endif /* HAVE_READV */
9504
Ross Lagerwall7807c352011-03-17 20:20:30 +02009505
9506#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10009507/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10009508os.pread
9509
9510 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09009511 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10009512 offset: Py_off_t
9513 /
9514
9515Read a number of bytes from a file descriptor starting at a particular offset.
9516
9517Read length bytes from file descriptor fd, starting at offset bytes from
9518the beginning of the file. The file offset remains unchanged.
9519[clinic start generated code]*/
9520
Larry Hastings2f936352014-08-05 14:04:04 +10009521static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09009522os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
9523/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009524{
Ross Lagerwall7807c352011-03-17 20:20:30 +02009525 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009526 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009527 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009528
Larry Hastings2f936352014-08-05 14:04:04 +10009529 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009530 errno = EINVAL;
9531 return posix_error();
9532 }
Larry Hastings2f936352014-08-05 14:04:04 +10009533 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009534 if (buffer == NULL)
9535 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009536
9537 do {
9538 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009539 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009540 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009541 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009542 Py_END_ALLOW_THREADS
9543 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9544
Ross Lagerwall7807c352011-03-17 20:20:30 +02009545 if (n < 0) {
9546 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009547 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009548 }
Larry Hastings2f936352014-08-05 14:04:04 +10009549 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02009550 _PyBytes_Resize(&buffer, n);
9551 return buffer;
9552}
Larry Hastings2f936352014-08-05 14:04:04 +10009553#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009554
Pablo Galindo4defba32018-01-27 16:16:37 +00009555#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
9556/*[clinic input]
9557os.preadv -> Py_ssize_t
9558
9559 fd: int
9560 buffers: object
9561 offset: Py_off_t
9562 flags: int = 0
9563 /
9564
9565Reads from a file descriptor into a number of mutable bytes-like objects.
9566
9567Combines the functionality of readv() and pread(). As readv(), it will
9568transfer data into each buffer until it is full and then move on to the next
9569buffer in the sequence to hold the rest of the data. Its fourth argument,
9570specifies the file offset at which the input operation is to be performed. It
9571will return the total number of bytes read (which can be less than the total
9572capacity of all the objects).
9573
9574The flags argument contains a bitwise OR of zero or more of the following flags:
9575
9576- RWF_HIPRI
9577- RWF_NOWAIT
9578
9579Using non-zero flags requires Linux 4.6 or newer.
9580[clinic start generated code]*/
9581
9582static Py_ssize_t
9583os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9584 int flags)
9585/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9586{
9587 Py_ssize_t cnt, n;
9588 int async_err = 0;
9589 struct iovec *iov;
9590 Py_buffer *buf;
9591
9592 if (!PySequence_Check(buffers)) {
9593 PyErr_SetString(PyExc_TypeError,
9594 "preadv2() arg 2 must be a sequence");
9595 return -1;
9596 }
9597
9598 cnt = PySequence_Size(buffers);
9599 if (cnt < 0) {
9600 return -1;
9601 }
9602
9603#ifndef HAVE_PREADV2
9604 if(flags != 0) {
9605 argument_unavailable_error("preadv2", "flags");
9606 return -1;
9607 }
9608#endif
9609
9610 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9611 return -1;
9612 }
9613#ifdef HAVE_PREADV2
9614 do {
9615 Py_BEGIN_ALLOW_THREADS
9616 _Py_BEGIN_SUPPRESS_IPH
9617 n = preadv2(fd, iov, cnt, offset, flags);
9618 _Py_END_SUPPRESS_IPH
9619 Py_END_ALLOW_THREADS
9620 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9621#else
9622 do {
Ronald Oussoren41761932020-11-08 10:05:27 +01009623#ifdef __APPLE__
9624/* This entire function will be removed from the module dict when the API
9625 * is not available.
9626 */
9627#pragma clang diagnostic push
9628#pragma clang diagnostic ignored "-Wunguarded-availability"
9629#pragma clang diagnostic ignored "-Wunguarded-availability-new"
9630#endif
Pablo Galindo4defba32018-01-27 16:16:37 +00009631 Py_BEGIN_ALLOW_THREADS
9632 _Py_BEGIN_SUPPRESS_IPH
9633 n = preadv(fd, iov, cnt, offset);
9634 _Py_END_SUPPRESS_IPH
9635 Py_END_ALLOW_THREADS
9636 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ronald Oussoren41761932020-11-08 10:05:27 +01009637
9638#ifdef __APPLE__
9639#pragma clang diagnostic pop
9640#endif
9641
Pablo Galindo4defba32018-01-27 16:16:37 +00009642#endif
9643
9644 iov_cleanup(iov, buf, cnt);
9645 if (n < 0) {
9646 if (!async_err) {
9647 posix_error();
9648 }
9649 return -1;
9650 }
9651
9652 return n;
9653}
9654#endif /* HAVE_PREADV */
9655
Larry Hastings2f936352014-08-05 14:04:04 +10009656
9657/*[clinic input]
9658os.write -> Py_ssize_t
9659
9660 fd: int
9661 data: Py_buffer
9662 /
9663
9664Write a bytes object to a file descriptor.
9665[clinic start generated code]*/
9666
Larry Hastings2f936352014-08-05 14:04:04 +10009667static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009668os_write_impl(PyObject *module, int fd, Py_buffer *data)
9669/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009670{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009671 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009672}
9673
9674#ifdef HAVE_SENDFILE
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009675#ifdef __APPLE__
9676/*[clinic input]
9677os.sendfile
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009678
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009679 out_fd: int
9680 in_fd: int
9681 offset: Py_off_t
9682 count as sbytes: Py_off_t
9683 headers: object(c_default="NULL") = ()
9684 trailers: object(c_default="NULL") = ()
9685 flags: int = 0
9686
9687Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9688[clinic start generated code]*/
9689
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009690static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009691os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
9692 Py_off_t sbytes, PyObject *headers, PyObject *trailers,
9693 int flags)
9694/*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/
9695#elif defined(__FreeBSD__) || defined(__DragonFly__)
9696/*[clinic input]
9697os.sendfile
9698
9699 out_fd: int
9700 in_fd: int
9701 offset: Py_off_t
9702 count: Py_ssize_t
9703 headers: object(c_default="NULL") = ()
9704 trailers: object(c_default="NULL") = ()
9705 flags: int = 0
9706
9707Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9708[clinic start generated code]*/
9709
9710static PyObject *
9711os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
9712 Py_ssize_t count, PyObject *headers, PyObject *trailers,
9713 int flags)
9714/*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/
9715#else
9716/*[clinic input]
9717os.sendfile
9718
9719 out_fd: int
9720 in_fd: int
9721 offset as offobj: object
9722 count: Py_ssize_t
9723
9724Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9725[clinic start generated code]*/
9726
9727static PyObject *
9728os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
9729 Py_ssize_t count)
9730/*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/
9731#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009732{
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009733 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009734 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009735
9736#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9737#ifndef __APPLE__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009738 off_t sbytes;
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009739#endif
9740 Py_buffer *hbuf, *tbuf;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009741 struct sf_hdtr sf;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009742
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009743 sf.headers = NULL;
9744 sf.trailers = NULL;
9745
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009746 if (headers != NULL) {
9747 if (!PySequence_Check(headers)) {
9748 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009749 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009750 return NULL;
9751 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009752 Py_ssize_t i = PySequence_Size(headers);
9753 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009754 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009755 if (i > INT_MAX) {
9756 PyErr_SetString(PyExc_OverflowError,
9757 "sendfile() header is too large");
9758 return NULL;
9759 }
9760 if (i > 0) {
9761 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009762 if (iov_setup(&(sf.headers), &hbuf,
9763 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009764 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009765#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009766 for (i = 0; i < sf.hdr_cnt; i++) {
9767 Py_ssize_t blen = sf.headers[i].iov_len;
9768# define OFF_T_MAX 0x7fffffffffffffff
9769 if (sbytes >= OFF_T_MAX - blen) {
9770 PyErr_SetString(PyExc_OverflowError,
9771 "sendfile() header is too large");
9772 return NULL;
9773 }
9774 sbytes += blen;
9775 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009776#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009777 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009778 }
9779 }
9780 if (trailers != NULL) {
9781 if (!PySequence_Check(trailers)) {
9782 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009783 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009784 return NULL;
9785 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009786 Py_ssize_t i = PySequence_Size(trailers);
9787 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009788 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009789 if (i > INT_MAX) {
9790 PyErr_SetString(PyExc_OverflowError,
9791 "sendfile() trailer is too large");
9792 return NULL;
9793 }
9794 if (i > 0) {
9795 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009796 if (iov_setup(&(sf.trailers), &tbuf,
9797 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009798 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009799 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009800 }
9801 }
9802
Steve Dower8fc89802015-04-12 00:26:27 -04009803 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009804 do {
9805 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009806#ifdef __APPLE__
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009807 ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009808#else
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009809 ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009810#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009811 Py_END_ALLOW_THREADS
9812 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009813 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009814
9815 if (sf.headers != NULL)
9816 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9817 if (sf.trailers != NULL)
9818 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9819
9820 if (ret < 0) {
9821 if ((errno == EAGAIN) || (errno == EBUSY)) {
9822 if (sbytes != 0) {
9823 // some data has been sent
9824 goto done;
9825 }
9826 else {
9827 // no data has been sent; upper application is supposed
9828 // to retry on EAGAIN or EBUSY
9829 return posix_error();
9830 }
9831 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009832 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009833 }
9834 goto done;
9835
9836done:
9837 #if !defined(HAVE_LARGEFILE_SUPPORT)
9838 return Py_BuildValue("l", sbytes);
9839 #else
9840 return Py_BuildValue("L", sbytes);
9841 #endif
9842
9843#else
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009844#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009845 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009846 do {
9847 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009848 ret = sendfile(out_fd, in_fd, NULL, count);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009849 Py_END_ALLOW_THREADS
9850 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009851 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009852 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009853 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009854 }
9855#endif
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009856 off_t offset;
Larry Hastings2f936352014-08-05 14:04:04 +10009857 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009858 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009859
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009860#if defined(__sun) && defined(__SVR4)
9861 // On Solaris, sendfile raises EINVAL rather than returning 0
9862 // when the offset is equal or bigger than the in_fd size.
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009863 struct stat st;
9864
9865 do {
9866 Py_BEGIN_ALLOW_THREADS
Jakub Kulíkfa8c9e72020-09-09 21:29:42 +02009867 ret = fstat(in_fd, &st);
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009868 Py_END_ALLOW_THREADS
Jakub Kulíkfa8c9e72020-09-09 21:29:42 +02009869 } while (ret != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009870 if (ret < 0)
9871 return (!async_err) ? posix_error() : NULL;
9872
9873 if (offset >= st.st_size) {
9874 return Py_BuildValue("i", 0);
9875 }
Jakub Stasiakfd4ed572020-11-12 10:49:30 +01009876
9877 // On illumos specifically sendfile() may perform a partial write but
9878 // return -1/an error (in one confirmed case the destination socket
9879 // had a 5 second timeout set and errno was EAGAIN) and it's on the client
9880 // code to check if the offset parameter was modified by sendfile().
9881 //
9882 // We need this variable to track said change.
9883 off_t original_offset = offset;
Jakub Kulík8c0be6f2020-09-05 21:10:01 +02009884#endif
9885
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009886 do {
9887 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009888 ret = sendfile(out_fd, in_fd, &offset, count);
Jakub Stasiakfd4ed572020-11-12 10:49:30 +01009889#if defined(__sun) && defined(__SVR4)
9890 // This handles illumos-specific sendfile() partial write behavior,
9891 // see a comment above for more details.
9892 if (ret < 0 && offset != original_offset) {
9893 ret = offset - original_offset;
9894 }
9895#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009896 Py_END_ALLOW_THREADS
9897 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009898 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009899 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009900 return Py_BuildValue("n", ret);
9901#endif
9902}
Larry Hastings2f936352014-08-05 14:04:04 +10009903#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009904
Larry Hastings2f936352014-08-05 14:04:04 +10009905
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009906#if defined(__APPLE__)
9907/*[clinic input]
9908os._fcopyfile
9909
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009910 in_fd: int
9911 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009912 flags: int
9913 /
9914
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009915Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009916[clinic start generated code]*/
9917
9918static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009919os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9920/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009921{
9922 int ret;
9923
9924 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009925 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009926 Py_END_ALLOW_THREADS
9927 if (ret < 0)
9928 return posix_error();
9929 Py_RETURN_NONE;
9930}
9931#endif
9932
9933
Larry Hastings2f936352014-08-05 14:04:04 +10009934/*[clinic input]
9935os.fstat
9936
9937 fd : int
9938
9939Perform a stat system call on the given file descriptor.
9940
9941Like stat(), but for an open file descriptor.
9942Equivalent to os.stat(fd).
9943[clinic start generated code]*/
9944
Larry Hastings2f936352014-08-05 14:04:04 +10009945static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009946os_fstat_impl(PyObject *module, int fd)
9947/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009948{
Victor Stinner8c62be82010-05-06 00:08:46 +00009949 STRUCT_STAT st;
9950 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009951 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009952
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009953 do {
9954 Py_BEGIN_ALLOW_THREADS
9955 res = FSTAT(fd, &st);
9956 Py_END_ALLOW_THREADS
9957 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009958 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009959#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009960 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009961#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009962 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009963#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009964 }
Tim Peters5aa91602002-01-30 05:46:57 +00009965
Victor Stinner1c2fa782020-05-10 11:05:29 +02009966 return _pystat_fromstructstat(module, &st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009967}
9968
Larry Hastings2f936352014-08-05 14:04:04 +10009969
9970/*[clinic input]
9971os.isatty -> bool
9972 fd: int
9973 /
9974
9975Return True if the fd is connected to a terminal.
9976
9977Return True if the file descriptor is an open file descriptor
9978connected to the slave end of a terminal.
9979[clinic start generated code]*/
9980
Larry Hastings2f936352014-08-05 14:04:04 +10009981static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009982os_isatty_impl(PyObject *module, int fd)
9983/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009984{
Steve Dower8fc89802015-04-12 00:26:27 -04009985 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009986 _Py_BEGIN_SUPPRESS_IPH
9987 return_value = isatty(fd);
9988 _Py_END_SUPPRESS_IPH
9989 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009990}
9991
9992
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009993#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009994/*[clinic input]
9995os.pipe
9996
9997Create a pipe.
9998
9999Returns a tuple of two file descriptors:
10000 (read_fd, write_fd)
10001[clinic start generated code]*/
10002
Larry Hastings2f936352014-08-05 14:04:04 +100010003static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010004os_pipe_impl(PyObject *module)
10005/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +000010006{
Victor Stinner8c62be82010-05-06 00:08:46 +000010007 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +020010008#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +000010009 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +020010010 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +000010011 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +020010012#else
10013 int res;
10014#endif
10015
10016#ifdef MS_WINDOWS
10017 attr.nLength = sizeof(attr);
10018 attr.lpSecurityDescriptor = NULL;
10019 attr.bInheritHandle = FALSE;
10020
10021 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -080010022 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +020010023 ok = CreatePipe(&read, &write, &attr, 0);
10024 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -070010025 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
10026 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +020010027 if (fds[0] == -1 || fds[1] == -1) {
10028 CloseHandle(read);
10029 CloseHandle(write);
10030 ok = 0;
10031 }
10032 }
Steve Dowerc3630612016-11-19 18:41:16 -080010033 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +020010034 Py_END_ALLOW_THREADS
10035
Victor Stinner8c62be82010-05-06 00:08:46 +000010036 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +010010037 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +020010038#else
10039
10040#ifdef HAVE_PIPE2
10041 Py_BEGIN_ALLOW_THREADS
10042 res = pipe2(fds, O_CLOEXEC);
10043 Py_END_ALLOW_THREADS
10044
10045 if (res != 0 && errno == ENOSYS)
10046 {
10047#endif
10048 Py_BEGIN_ALLOW_THREADS
10049 res = pipe(fds);
10050 Py_END_ALLOW_THREADS
10051
10052 if (res == 0) {
10053 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
10054 close(fds[0]);
10055 close(fds[1]);
10056 return NULL;
10057 }
10058 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
10059 close(fds[0]);
10060 close(fds[1]);
10061 return NULL;
10062 }
10063 }
10064#ifdef HAVE_PIPE2
10065 }
10066#endif
10067
10068 if (res != 0)
10069 return PyErr_SetFromErrno(PyExc_OSError);
10070#endif /* !MS_WINDOWS */
10071 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +000010072}
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010073#endif /* HAVE_PIPE */
10074
Larry Hastings2f936352014-08-05 14:04:04 +100010075
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010076#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +100010077/*[clinic input]
10078os.pipe2
10079
10080 flags: int
10081 /
10082
10083Create a pipe with flags set atomically.
10084
10085Returns a tuple of two file descriptors:
10086 (read_fd, write_fd)
10087
10088flags can be constructed by ORing together one or more of these values:
10089O_NONBLOCK, O_CLOEXEC.
10090[clinic start generated code]*/
10091
Larry Hastings2f936352014-08-05 14:04:04 +100010092static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010093os_pipe2_impl(PyObject *module, int flags)
10094/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010095{
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010096 int fds[2];
10097 int res;
10098
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010099 res = pipe2(fds, flags);
10100 if (res != 0)
10101 return posix_error();
10102 return Py_BuildValue("(ii)", fds[0], fds[1]);
10103}
10104#endif /* HAVE_PIPE2 */
10105
Larry Hastings2f936352014-08-05 14:04:04 +100010106
Ross Lagerwall7807c352011-03-17 20:20:30 +020010107#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +100010108/*[clinic input]
10109os.writev -> Py_ssize_t
10110 fd: int
10111 buffers: object
10112 /
10113
10114Iterate over buffers, and write the contents of each to a file descriptor.
10115
10116Returns the total number of bytes written.
10117buffers must be a sequence of bytes-like objects.
10118[clinic start generated code]*/
10119
Larry Hastings2f936352014-08-05 14:04:04 +100010120static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010121os_writev_impl(PyObject *module, int fd, PyObject *buffers)
10122/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010123{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +030010124 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +100010125 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010126 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010127 struct iovec *iov;
10128 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +100010129
10130 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010131 PyErr_SetString(PyExc_TypeError,
10132 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +100010133 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010134 }
Larry Hastings2f936352014-08-05 14:04:04 +100010135 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +030010136 if (cnt < 0)
10137 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010138
Larry Hastings2f936352014-08-05 14:04:04 +100010139 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
10140 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010141 }
10142
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010143 do {
10144 Py_BEGIN_ALLOW_THREADS
10145 result = writev(fd, iov, cnt);
10146 Py_END_ALLOW_THREADS
10147 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +020010148
10149 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010150 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +100010151 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +010010152
Georg Brandl306336b2012-06-24 12:55:33 +020010153 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010154}
Larry Hastings2f936352014-08-05 14:04:04 +100010155#endif /* HAVE_WRITEV */
10156
10157
10158#ifdef HAVE_PWRITE
10159/*[clinic input]
10160os.pwrite -> Py_ssize_t
10161
10162 fd: int
10163 buffer: Py_buffer
10164 offset: Py_off_t
10165 /
10166
10167Write bytes to a file descriptor starting at a particular offset.
10168
10169Write buffer to fd, starting at offset bytes from the beginning of
10170the file. Returns the number of bytes writte. Does not change the
10171current file offset.
10172[clinic start generated code]*/
10173
Larry Hastings2f936352014-08-05 14:04:04 +100010174static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010175os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
10176/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010177{
10178 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010179 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010180
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010181 do {
10182 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -040010183 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010184 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -040010185 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010186 Py_END_ALLOW_THREADS
10187 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010188
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010189 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +100010190 posix_error();
10191 return size;
10192}
10193#endif /* HAVE_PWRITE */
10194
Pablo Galindo4defba32018-01-27 16:16:37 +000010195#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
10196/*[clinic input]
10197os.pwritev -> Py_ssize_t
10198
10199 fd: int
10200 buffers: object
10201 offset: Py_off_t
10202 flags: int = 0
10203 /
10204
10205Writes the contents of bytes-like objects to a file descriptor at a given offset.
10206
10207Combines the functionality of writev() and pwrite(). All buffers must be a sequence
10208of bytes-like objects. Buffers are processed in array order. Entire contents of first
10209buffer is written before proceeding to second, and so on. The operating system may
10210set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
10211This function writes the contents of each object to the file descriptor and returns
10212the total number of bytes written.
10213
10214The flags argument contains a bitwise OR of zero or more of the following flags:
10215
10216- RWF_DSYNC
10217- RWF_SYNC
YoSTEALTH76ef2552020-05-27 15:32:22 -060010218- RWF_APPEND
Pablo Galindo4defba32018-01-27 16:16:37 +000010219
10220Using non-zero flags requires Linux 4.7 or newer.
10221[clinic start generated code]*/
10222
10223static Py_ssize_t
10224os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
10225 int flags)
YoSTEALTH76ef2552020-05-27 15:32:22 -060010226/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/
Pablo Galindo4defba32018-01-27 16:16:37 +000010227{
10228 Py_ssize_t cnt;
10229 Py_ssize_t result;
10230 int async_err = 0;
10231 struct iovec *iov;
10232 Py_buffer *buf;
10233
10234 if (!PySequence_Check(buffers)) {
10235 PyErr_SetString(PyExc_TypeError,
10236 "pwritev() arg 2 must be a sequence");
10237 return -1;
10238 }
10239
10240 cnt = PySequence_Size(buffers);
10241 if (cnt < 0) {
10242 return -1;
10243 }
10244
10245#ifndef HAVE_PWRITEV2
10246 if(flags != 0) {
10247 argument_unavailable_error("pwritev2", "flags");
10248 return -1;
10249 }
10250#endif
10251
10252 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
10253 return -1;
10254 }
10255#ifdef HAVE_PWRITEV2
10256 do {
10257 Py_BEGIN_ALLOW_THREADS
10258 _Py_BEGIN_SUPPRESS_IPH
10259 result = pwritev2(fd, iov, cnt, offset, flags);
10260 _Py_END_SUPPRESS_IPH
10261 Py_END_ALLOW_THREADS
10262 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10263#else
Ronald Oussoren41761932020-11-08 10:05:27 +010010264
10265#ifdef __APPLE__
10266/* This entire function will be removed from the module dict when the API
10267 * is not available.
10268 */
10269#pragma clang diagnostic push
10270#pragma clang diagnostic ignored "-Wunguarded-availability"
10271#pragma clang diagnostic ignored "-Wunguarded-availability-new"
10272#endif
Pablo Galindo4defba32018-01-27 16:16:37 +000010273 do {
10274 Py_BEGIN_ALLOW_THREADS
10275 _Py_BEGIN_SUPPRESS_IPH
10276 result = pwritev(fd, iov, cnt, offset);
10277 _Py_END_SUPPRESS_IPH
10278 Py_END_ALLOW_THREADS
10279 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ronald Oussoren41761932020-11-08 10:05:27 +010010280
10281#ifdef __APPLE__
10282#pragma clang diagnostic pop
10283#endif
10284
Pablo Galindo4defba32018-01-27 16:16:37 +000010285#endif
10286
10287 iov_cleanup(iov, buf, cnt);
10288 if (result < 0) {
10289 if (!async_err) {
10290 posix_error();
10291 }
10292 return -1;
10293 }
10294
10295 return result;
10296}
10297#endif /* HAVE_PWRITEV */
10298
Pablo Galindoaac4d032019-05-31 19:39:47 +010010299#ifdef HAVE_COPY_FILE_RANGE
10300/*[clinic input]
10301
10302os.copy_file_range
10303 src: int
10304 Source file descriptor.
10305 dst: int
10306 Destination file descriptor.
10307 count: Py_ssize_t
10308 Number of bytes to copy.
10309 offset_src: object = None
10310 Starting offset in src.
10311 offset_dst: object = None
10312 Starting offset in dst.
10313
10314Copy count bytes from one file descriptor to another.
10315
10316If offset_src is None, then src is read from the current position;
10317respectively for offset_dst.
10318[clinic start generated code]*/
10319
10320static PyObject *
10321os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
10322 PyObject *offset_src, PyObject *offset_dst)
10323/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
10324{
10325 off_t offset_src_val, offset_dst_val;
10326 off_t *p_offset_src = NULL;
10327 off_t *p_offset_dst = NULL;
10328 Py_ssize_t ret;
10329 int async_err = 0;
10330 /* The flags argument is provided to allow
10331 * for future extensions and currently must be to 0. */
10332 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +000010333
10334
Pablo Galindoaac4d032019-05-31 19:39:47 +010010335 if (count < 0) {
10336 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
10337 return NULL;
10338 }
10339
10340 if (offset_src != Py_None) {
10341 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
10342 return NULL;
10343 }
10344 p_offset_src = &offset_src_val;
10345 }
10346
10347 if (offset_dst != Py_None) {
10348 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
10349 return NULL;
10350 }
10351 p_offset_dst = &offset_dst_val;
10352 }
10353
10354 do {
10355 Py_BEGIN_ALLOW_THREADS
10356 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
10357 Py_END_ALLOW_THREADS
10358 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10359
10360 if (ret < 0) {
10361 return (!async_err) ? posix_error() : NULL;
10362 }
10363
10364 return PyLong_FromSsize_t(ret);
10365}
10366#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +100010367
10368#ifdef HAVE_MKFIFO
10369/*[clinic input]
10370os.mkfifo
10371
10372 path: path_t
10373 mode: int=0o666
10374 *
10375 dir_fd: dir_fd(requires='mkfifoat')=None
10376
10377Create a "fifo" (a POSIX named pipe).
10378
10379If dir_fd is not None, it should be a file descriptor open to a directory,
10380 and path should be relative; path will then be relative to that directory.
10381dir_fd may not be implemented on your platform.
10382 If it is unavailable, using it will raise a NotImplementedError.
10383[clinic start generated code]*/
10384
Larry Hastings2f936352014-08-05 14:04:04 +100010385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010386os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
10387/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010388{
10389 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010390 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010391
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010392 do {
10393 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +100010394#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010395 if (dir_fd != DEFAULT_DIR_FD)
10396 result = mkfifoat(dir_fd, path->narrow, mode);
10397 else
Ross Lagerwall7807c352011-03-17 20:20:30 +020010398#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010399 result = mkfifo(path->narrow, mode);
10400 Py_END_ALLOW_THREADS
10401 } while (result != 0 && errno == EINTR &&
10402 !(async_err = PyErr_CheckSignals()));
10403 if (result != 0)
10404 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010405
10406 Py_RETURN_NONE;
10407}
10408#endif /* HAVE_MKFIFO */
10409
10410
10411#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
10412/*[clinic input]
10413os.mknod
10414
10415 path: path_t
10416 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010417 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +100010418 *
10419 dir_fd: dir_fd(requires='mknodat')=None
10420
10421Create a node in the file system.
10422
10423Create a node in the file system (file, device special file or named pipe)
10424at path. mode specifies both the permissions to use and the
10425type of node to be created, being combined (bitwise OR) with one of
10426S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
10427device defines the newly created device special file (probably using
10428os.makedev()). Otherwise device is ignored.
10429
10430If dir_fd is not None, it should be a file descriptor open to a directory,
10431 and path should be relative; path will then be relative to that directory.
10432dir_fd may not be implemented on your platform.
10433 If it is unavailable, using it will raise a NotImplementedError.
10434[clinic start generated code]*/
10435
Larry Hastings2f936352014-08-05 14:04:04 +100010436static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010437os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -040010438 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010439/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010440{
10441 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010442 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010443
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010444 do {
10445 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +100010446#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010447 if (dir_fd != DEFAULT_DIR_FD)
10448 result = mknodat(dir_fd, path->narrow, mode, device);
10449 else
Larry Hastings2f936352014-08-05 14:04:04 +100010450#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010451 result = mknod(path->narrow, mode, device);
10452 Py_END_ALLOW_THREADS
10453 } while (result != 0 && errno == EINTR &&
10454 !(async_err = PyErr_CheckSignals()));
10455 if (result != 0)
10456 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010457
10458 Py_RETURN_NONE;
10459}
10460#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
10461
10462
10463#ifdef HAVE_DEVICE_MACROS
10464/*[clinic input]
10465os.major -> unsigned_int
10466
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010467 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010468 /
10469
10470Extracts a device major number from a raw device number.
10471[clinic start generated code]*/
10472
Larry Hastings2f936352014-08-05 14:04:04 +100010473static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010474os_major_impl(PyObject *module, dev_t device)
10475/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010476{
10477 return major(device);
10478}
10479
10480
10481/*[clinic input]
10482os.minor -> unsigned_int
10483
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010484 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010485 /
10486
10487Extracts a device minor number from a raw device number.
10488[clinic start generated code]*/
10489
Larry Hastings2f936352014-08-05 14:04:04 +100010490static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010491os_minor_impl(PyObject *module, dev_t device)
10492/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010493{
10494 return minor(device);
10495}
10496
10497
10498/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010499os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010500
10501 major: int
10502 minor: int
10503 /
10504
10505Composes a raw device number from the major and minor device numbers.
10506[clinic start generated code]*/
10507
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010508static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010509os_makedev_impl(PyObject *module, int major, int minor)
10510/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010511{
10512 return makedev(major, minor);
10513}
10514#endif /* HAVE_DEVICE_MACROS */
10515
10516
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010517#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010518/*[clinic input]
10519os.ftruncate
10520
10521 fd: int
10522 length: Py_off_t
10523 /
10524
10525Truncate a file, specified by file descriptor, to a specific length.
10526[clinic start generated code]*/
10527
Larry Hastings2f936352014-08-05 14:04:04 +100010528static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010529os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
10530/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010531{
10532 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010533 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010534
Steve Dowerb82e17e2019-05-23 08:45:22 -070010535 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
10536 return NULL;
10537 }
10538
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010539 do {
10540 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010541 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010542#ifdef MS_WINDOWS
10543 result = _chsize_s(fd, length);
10544#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010545 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010546#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010547 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010548 Py_END_ALLOW_THREADS
10549 } while (result != 0 && errno == EINTR &&
10550 !(async_err = PyErr_CheckSignals()));
10551 if (result != 0)
10552 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010553 Py_RETURN_NONE;
10554}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010555#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010556
10557
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010558#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010559/*[clinic input]
10560os.truncate
10561 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
10562 length: Py_off_t
10563
10564Truncate a file, specified by path, to a specific length.
10565
10566On some platforms, path may also be specified as an open file descriptor.
10567 If this functionality is unavailable, using it raises an exception.
10568[clinic start generated code]*/
10569
Larry Hastings2f936352014-08-05 14:04:04 +100010570static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010571os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
10572/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010573{
10574 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010575#ifdef MS_WINDOWS
10576 int fd;
10577#endif
10578
10579 if (path->fd != -1)
10580 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010581
Steve Dowerb82e17e2019-05-23 08:45:22 -070010582 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
10583 return NULL;
10584 }
10585
Larry Hastings2f936352014-08-05 14:04:04 +100010586 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010587 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010588#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070010589 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +020010590 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010591 result = -1;
10592 else {
10593 result = _chsize_s(fd, length);
10594 close(fd);
10595 if (result < 0)
10596 errno = result;
10597 }
10598#else
10599 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010600#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010601 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +100010602 Py_END_ALLOW_THREADS
10603 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +030010604 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +100010605
10606 Py_RETURN_NONE;
10607}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010608#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010609
Ross Lagerwall7807c352011-03-17 20:20:30 +020010610
Victor Stinnerd6b17692014-09-30 12:20:05 +020010611/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
10612 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
10613 defined, which is the case in Python on AIX. AIX bug report:
10614 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
10615#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
10616# define POSIX_FADVISE_AIX_BUG
10617#endif
10618
Victor Stinnerec39e262014-09-30 12:35:58 +020010619
Victor Stinnerd6b17692014-09-30 12:20:05 +020010620#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010621/*[clinic input]
10622os.posix_fallocate
10623
10624 fd: int
10625 offset: Py_off_t
10626 length: Py_off_t
10627 /
10628
10629Ensure a file has allocated at least a particular number of bytes on disk.
10630
10631Ensure that the file specified by fd encompasses a range of bytes
10632starting at offset bytes from the beginning and continuing for length bytes.
10633[clinic start generated code]*/
10634
Larry Hastings2f936352014-08-05 14:04:04 +100010635static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010636os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010637 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010638/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010639{
10640 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010641 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010642
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010643 do {
10644 Py_BEGIN_ALLOW_THREADS
10645 result = posix_fallocate(fd, offset, length);
10646 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010647 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10648
10649 if (result == 0)
10650 Py_RETURN_NONE;
10651
10652 if (async_err)
10653 return NULL;
10654
10655 errno = result;
10656 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010657}
Victor Stinnerec39e262014-09-30 12:35:58 +020010658#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +100010659
Ross Lagerwall7807c352011-03-17 20:20:30 +020010660
Victor Stinnerd6b17692014-09-30 12:20:05 +020010661#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010662/*[clinic input]
10663os.posix_fadvise
10664
10665 fd: int
10666 offset: Py_off_t
10667 length: Py_off_t
10668 advice: int
10669 /
10670
10671Announce an intention to access data in a specific pattern.
10672
10673Announce an intention to access data in a specific pattern, thus allowing
10674the kernel to make optimizations.
10675The advice applies to the region of the file specified by fd starting at
10676offset and continuing for length bytes.
10677advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10678POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10679POSIX_FADV_DONTNEED.
10680[clinic start generated code]*/
10681
Larry Hastings2f936352014-08-05 14:04:04 +100010682static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010683os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010684 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010685/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010686{
10687 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010688 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010689
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010690 do {
10691 Py_BEGIN_ALLOW_THREADS
10692 result = posix_fadvise(fd, offset, length, advice);
10693 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010694 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10695
10696 if (result == 0)
10697 Py_RETURN_NONE;
10698
10699 if (async_err)
10700 return NULL;
10701
10702 errno = result;
10703 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010704}
Victor Stinnerec39e262014-09-30 12:35:58 +020010705#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010706
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010707
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010708#ifdef MS_WINDOWS
Victor Stinner161e7b32020-01-24 11:53:44 +010010709static PyObject*
10710win32_putenv(PyObject *name, PyObject *value)
10711{
10712 /* Search from index 1 because on Windows starting '=' is allowed for
10713 defining hidden environment variables. */
10714 if (PyUnicode_GET_LENGTH(name) == 0 ||
10715 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10716 {
10717 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10718 return NULL;
10719 }
10720 PyObject *unicode;
10721 if (value != NULL) {
10722 unicode = PyUnicode_FromFormat("%U=%U", name, value);
10723 }
10724 else {
10725 unicode = PyUnicode_FromFormat("%U=", name);
10726 }
10727 if (unicode == NULL) {
10728 return NULL;
10729 }
10730
10731 Py_ssize_t size;
10732 /* PyUnicode_AsWideCharString() rejects embedded null characters */
10733 wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
10734 Py_DECREF(unicode);
10735
10736 if (env == NULL) {
10737 return NULL;
10738 }
10739 if (size > _MAX_ENV) {
10740 PyErr_Format(PyExc_ValueError,
10741 "the environment variable is longer than %u characters",
10742 _MAX_ENV);
10743 PyMem_Free(env);
10744 return NULL;
10745 }
10746
10747 /* _wputenv() and SetEnvironmentVariableW() update the environment in the
10748 Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
10749 and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
10750
10751 Prefer _wputenv() to be compatible with C libraries using CRT
10752 variables and CRT functions using these variables (ex: getenv()). */
10753 int err = _wputenv(env);
10754 PyMem_Free(env);
10755
10756 if (err) {
10757 posix_error();
10758 return NULL;
10759 }
10760
10761 Py_RETURN_NONE;
10762}
10763#endif
10764
10765
10766#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010767/*[clinic input]
10768os.putenv
10769
10770 name: unicode
10771 value: unicode
10772 /
10773
10774Change or add an environment variable.
10775[clinic start generated code]*/
10776
Larry Hastings2f936352014-08-05 14:04:04 +100010777static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010778os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10779/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010780{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010781 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10782 return NULL;
10783 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010784 return win32_putenv(name, value);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010785}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010786#else
Larry Hastings2f936352014-08-05 14:04:04 +100010787/*[clinic input]
10788os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010789
Larry Hastings2f936352014-08-05 14:04:04 +100010790 name: FSConverter
10791 value: FSConverter
10792 /
10793
10794Change or add an environment variable.
10795[clinic start generated code]*/
10796
Larry Hastings2f936352014-08-05 14:04:04 +100010797static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010798os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10799/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010800{
Serhiy Storchaka77703942017-06-25 07:33:01 +030010801 const char *name_string = PyBytes_AS_STRING(name);
10802 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010803
Serhiy Storchaka77703942017-06-25 07:33:01 +030010804 if (strchr(name_string, '=') != NULL) {
10805 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10806 return NULL;
10807 }
Victor Stinnerb477d192020-01-22 22:48:16 +010010808
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010809 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10810 return NULL;
10811 }
10812
Victor Stinnerb477d192020-01-22 22:48:16 +010010813 if (setenv(name_string, value_string, 1)) {
10814 return posix_error();
10815 }
Larry Hastings2f936352014-08-05 14:04:04 +100010816 Py_RETURN_NONE;
10817}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010818#endif /* !defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100010819
10820
Victor Stinner161e7b32020-01-24 11:53:44 +010010821#ifdef MS_WINDOWS
10822/*[clinic input]
10823os.unsetenv
10824 name: unicode
10825 /
10826
10827Delete an environment variable.
10828[clinic start generated code]*/
10829
10830static PyObject *
10831os_unsetenv_impl(PyObject *module, PyObject *name)
10832/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10833{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010834 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10835 return NULL;
10836 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010837 return win32_putenv(name, NULL);
10838}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010839#else
Larry Hastings2f936352014-08-05 14:04:04 +100010840/*[clinic input]
10841os.unsetenv
10842 name: FSConverter
10843 /
10844
10845Delete an environment variable.
10846[clinic start generated code]*/
10847
Larry Hastings2f936352014-08-05 14:04:04 +100010848static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010849os_unsetenv_impl(PyObject *module, PyObject *name)
10850/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010851{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010852 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10853 return NULL;
10854 }
Victor Stinner984890f2011-11-24 13:53:38 +010010855#ifdef HAVE_BROKEN_UNSETENV
10856 unsetenv(PyBytes_AS_STRING(name));
10857#else
Victor Stinner161e7b32020-01-24 11:53:44 +010010858 int err = unsetenv(PyBytes_AS_STRING(name));
10859 if (err) {
Victor Stinner60b385e2011-11-22 22:01:28 +010010860 return posix_error();
Victor Stinner161e7b32020-01-24 11:53:44 +010010861 }
Victor Stinner984890f2011-11-24 13:53:38 +010010862#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010863
Victor Stinner84ae1182010-05-06 22:05:07 +000010864 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010865}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010866#endif /* !MS_WINDOWS */
Guido van Rossumc524d952001-10-19 01:31:59 +000010867
Larry Hastings2f936352014-08-05 14:04:04 +100010868
10869/*[clinic input]
10870os.strerror
10871
10872 code: int
10873 /
10874
10875Translate an error code to a message string.
10876[clinic start generated code]*/
10877
Larry Hastings2f936352014-08-05 14:04:04 +100010878static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010879os_strerror_impl(PyObject *module, int code)
10880/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010881{
10882 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010883 if (message == NULL) {
10884 PyErr_SetString(PyExc_ValueError,
10885 "strerror() argument out of range");
10886 return NULL;
10887 }
Victor Stinner1b579672011-12-17 05:47:23 +010010888 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010889}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010890
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010891
Guido van Rossumc9641791998-08-04 15:26:23 +000010892#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010893#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010894/*[clinic input]
10895os.WCOREDUMP -> bool
10896
10897 status: int
10898 /
10899
10900Return True if the process returning status was dumped to a core file.
10901[clinic start generated code]*/
10902
Larry Hastings2f936352014-08-05 14:04:04 +100010903static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010904os_WCOREDUMP_impl(PyObject *module, int status)
10905/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010906{
10907 WAIT_TYPE wait_status;
10908 WAIT_STATUS_INT(wait_status) = status;
10909 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010910}
10911#endif /* WCOREDUMP */
10912
Larry Hastings2f936352014-08-05 14:04:04 +100010913
Fred Drake106c1a02002-04-23 15:58:02 +000010914#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010915/*[clinic input]
10916os.WIFCONTINUED -> bool
10917
10918 status: int
10919
10920Return True if a particular process was continued from a job control stop.
10921
10922Return True if the process returning status was continued from a
10923job control stop.
10924[clinic start generated code]*/
10925
Larry Hastings2f936352014-08-05 14:04:04 +100010926static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010927os_WIFCONTINUED_impl(PyObject *module, int status)
10928/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010929{
10930 WAIT_TYPE wait_status;
10931 WAIT_STATUS_INT(wait_status) = status;
10932 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010933}
10934#endif /* WIFCONTINUED */
10935
Larry Hastings2f936352014-08-05 14:04:04 +100010936
Guido van Rossumc9641791998-08-04 15:26:23 +000010937#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010938/*[clinic input]
10939os.WIFSTOPPED -> bool
10940
10941 status: int
10942
10943Return True if the process returning status was stopped.
10944[clinic start generated code]*/
10945
Larry Hastings2f936352014-08-05 14:04:04 +100010946static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010947os_WIFSTOPPED_impl(PyObject *module, int status)
10948/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010949{
10950 WAIT_TYPE wait_status;
10951 WAIT_STATUS_INT(wait_status) = status;
10952 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010953}
10954#endif /* WIFSTOPPED */
10955
Larry Hastings2f936352014-08-05 14:04:04 +100010956
Guido van Rossumc9641791998-08-04 15:26:23 +000010957#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010958/*[clinic input]
10959os.WIFSIGNALED -> bool
10960
10961 status: int
10962
10963Return True if the process returning status was terminated by a signal.
10964[clinic start generated code]*/
10965
Larry Hastings2f936352014-08-05 14:04:04 +100010966static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010967os_WIFSIGNALED_impl(PyObject *module, int status)
10968/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010969{
10970 WAIT_TYPE wait_status;
10971 WAIT_STATUS_INT(wait_status) = status;
10972 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010973}
10974#endif /* WIFSIGNALED */
10975
Larry Hastings2f936352014-08-05 14:04:04 +100010976
Guido van Rossumc9641791998-08-04 15:26:23 +000010977#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010978/*[clinic input]
10979os.WIFEXITED -> bool
10980
10981 status: int
10982
10983Return True if the process returning status exited via the exit() system call.
10984[clinic start generated code]*/
10985
Larry Hastings2f936352014-08-05 14:04:04 +100010986static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010987os_WIFEXITED_impl(PyObject *module, int status)
10988/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010989{
10990 WAIT_TYPE wait_status;
10991 WAIT_STATUS_INT(wait_status) = status;
10992 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010993}
10994#endif /* WIFEXITED */
10995
Larry Hastings2f936352014-08-05 14:04:04 +100010996
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010997#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010998/*[clinic input]
10999os.WEXITSTATUS -> int
11000
11001 status: int
11002
11003Return the process return code from status.
11004[clinic start generated code]*/
11005
Larry Hastings2f936352014-08-05 14:04:04 +100011006static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011007os_WEXITSTATUS_impl(PyObject *module, int status)
11008/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011009{
11010 WAIT_TYPE wait_status;
11011 WAIT_STATUS_INT(wait_status) = status;
11012 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011013}
11014#endif /* WEXITSTATUS */
11015
Larry Hastings2f936352014-08-05 14:04:04 +100011016
Guido van Rossumc9641791998-08-04 15:26:23 +000011017#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100011018/*[clinic input]
11019os.WTERMSIG -> int
11020
11021 status: int
11022
11023Return the signal that terminated the process that provided the status value.
11024[clinic start generated code]*/
11025
Larry Hastings2f936352014-08-05 14:04:04 +100011026static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011027os_WTERMSIG_impl(PyObject *module, int status)
11028/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011029{
11030 WAIT_TYPE wait_status;
11031 WAIT_STATUS_INT(wait_status) = status;
11032 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011033}
11034#endif /* WTERMSIG */
11035
Larry Hastings2f936352014-08-05 14:04:04 +100011036
Guido van Rossumc9641791998-08-04 15:26:23 +000011037#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100011038/*[clinic input]
11039os.WSTOPSIG -> int
11040
11041 status: int
11042
11043Return the signal that stopped the process that provided the status value.
11044[clinic start generated code]*/
11045
Larry Hastings2f936352014-08-05 14:04:04 +100011046static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011047os_WSTOPSIG_impl(PyObject *module, int status)
11048/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011049{
11050 WAIT_TYPE wait_status;
11051 WAIT_STATUS_INT(wait_status) = status;
11052 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011053}
11054#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000011055#endif /* HAVE_SYS_WAIT_H */
11056
11057
Thomas Wouters477c8d52006-05-27 19:21:47 +000011058#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000011059#ifdef _SCO_DS
11060/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
11061 needed definitions in sys/statvfs.h */
11062#define _SVID3
11063#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011064#include <sys/statvfs.h>
11065
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011066static PyObject*
Victor Stinner1c2fa782020-05-10 11:05:29 +020011067_pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) {
11068 PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080011069 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 if (v == NULL)
11071 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011072
11073#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000011074 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
11075 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
11076 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
11077 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
11078 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
11079 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
11080 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
11081 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
11082 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
11083 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011084#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
11086 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
11087 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011088 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000011089 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011090 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011092 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000011093 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011094 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000011095 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011096 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011098 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
11100 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011101#endif
Michael Felt502d5512018-01-05 13:01:58 +010011102/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
11103 * (issue #32390). */
11104#if defined(_AIX) && defined(_ALL_SOURCE)
11105 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
11106#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010011107 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010011108#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010011109 if (PyErr_Occurred()) {
11110 Py_DECREF(v);
11111 return NULL;
11112 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011113
Victor Stinner8c62be82010-05-06 00:08:46 +000011114 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011115}
11116
Larry Hastings2f936352014-08-05 14:04:04 +100011117
11118/*[clinic input]
11119os.fstatvfs
11120 fd: int
11121 /
11122
11123Perform an fstatvfs system call on the given fd.
11124
11125Equivalent to statvfs(fd).
11126[clinic start generated code]*/
11127
Larry Hastings2f936352014-08-05 14:04:04 +100011128static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011129os_fstatvfs_impl(PyObject *module, int fd)
11130/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011131{
11132 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011133 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011134 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011135
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011136 do {
11137 Py_BEGIN_ALLOW_THREADS
11138 result = fstatvfs(fd, &st);
11139 Py_END_ALLOW_THREADS
11140 } while (result != 0 && errno == EINTR &&
11141 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100011142 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011143 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011144
Victor Stinner1c2fa782020-05-10 11:05:29 +020011145 return _pystatvfs_fromstructstatvfs(module, st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000011146}
Larry Hastings2f936352014-08-05 14:04:04 +100011147#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000011148
11149
Thomas Wouters477c8d52006-05-27 19:21:47 +000011150#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000011151#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100011152/*[clinic input]
11153os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000011154
Larry Hastings2f936352014-08-05 14:04:04 +100011155 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
11156
11157Perform a statvfs system call on the given path.
11158
11159path may always be specified as a string.
11160On some platforms, path may also be specified as an open file descriptor.
11161 If this functionality is unavailable, using it raises an exception.
11162[clinic start generated code]*/
11163
Larry Hastings2f936352014-08-05 14:04:04 +100011164static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011165os_statvfs_impl(PyObject *module, path_t *path)
11166/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011167{
11168 int result;
11169 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011170
11171 Py_BEGIN_ALLOW_THREADS
11172#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100011173 if (path->fd != -1) {
Larry Hastings2f936352014-08-05 14:04:04 +100011174 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011175 }
11176 else
11177#endif
Larry Hastings2f936352014-08-05 14:04:04 +100011178 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011179 Py_END_ALLOW_THREADS
11180
11181 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011182 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011183 }
11184
Victor Stinner1c2fa782020-05-10 11:05:29 +020011185 return _pystatvfs_fromstructstatvfs(module, st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000011186}
Larry Hastings2f936352014-08-05 14:04:04 +100011187#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
11188
Guido van Rossum94f6f721999-01-06 18:42:14 +000011189
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011190#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011191/*[clinic input]
11192os._getdiskusage
11193
Steve Dower23ad6d02018-02-22 10:39:10 -080011194 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100011195
11196Return disk usage statistics about the given path as a (total, free) tuple.
11197[clinic start generated code]*/
11198
Larry Hastings2f936352014-08-05 14:04:04 +100011199static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080011200os__getdiskusage_impl(PyObject *module, path_t *path)
11201/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011202{
11203 BOOL retval;
11204 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040011205 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011206
11207 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080011208 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011209 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040011210 if (retval == 0) {
11211 if (GetLastError() == ERROR_DIRECTORY) {
11212 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011213
Joe Pamerc8c02492018-09-25 10:57:36 -040011214 dir_path = PyMem_New(wchar_t, path->length + 1);
11215 if (dir_path == NULL) {
11216 return PyErr_NoMemory();
11217 }
11218
11219 wcscpy_s(dir_path, path->length + 1, path->wide);
11220
11221 if (_dirnameW(dir_path) != -1) {
11222 Py_BEGIN_ALLOW_THREADS
11223 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
11224 Py_END_ALLOW_THREADS
11225 }
11226 /* Record the last error in case it's modified by PyMem_Free. */
11227 err = GetLastError();
11228 PyMem_Free(dir_path);
11229 if (retval) {
11230 goto success;
11231 }
11232 }
11233 return PyErr_SetFromWindowsErr(err);
11234 }
11235
11236success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011237 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
11238}
Larry Hastings2f936352014-08-05 14:04:04 +100011239#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011240
11241
Fred Drakec9680921999-12-13 16:37:25 +000011242/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
11243 * It maps strings representing configuration variable names to
11244 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000011245 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000011246 * rarely-used constants. There are three separate tables that use
11247 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000011248 *
11249 * This code is always included, even if none of the interfaces that
11250 * need it are included. The #if hackery needed to avoid it would be
11251 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000011252 */
11253struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011254 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030011255 int value;
Fred Drakec9680921999-12-13 16:37:25 +000011256};
11257
Fred Drake12c6e2d1999-12-14 21:25:03 +000011258static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011259conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000011260 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000011261{
Christian Heimes217cfd12007-12-02 14:31:20 +000011262 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030011263 int value = _PyLong_AsInt(arg);
11264 if (value == -1 && PyErr_Occurred())
11265 return 0;
11266 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000011267 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000011268 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000011269 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000011270 /* look up the value in the table using a binary search */
11271 size_t lo = 0;
11272 size_t mid;
11273 size_t hi = tablesize;
11274 int cmp;
11275 const char *confname;
11276 if (!PyUnicode_Check(arg)) {
11277 PyErr_SetString(PyExc_TypeError,
11278 "configuration names must be strings or integers");
11279 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011280 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020011281 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000011282 if (confname == NULL)
11283 return 0;
11284 while (lo < hi) {
11285 mid = (lo + hi) / 2;
11286 cmp = strcmp(confname, table[mid].name);
11287 if (cmp < 0)
11288 hi = mid;
11289 else if (cmp > 0)
11290 lo = mid + 1;
11291 else {
11292 *valuep = table[mid].value;
11293 return 1;
11294 }
11295 }
11296 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
11297 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011298 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000011299}
11300
11301
11302#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
11303static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000011304#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011305 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011306#endif
11307#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011308 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011309#endif
Fred Drakec9680921999-12-13 16:37:25 +000011310#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011311 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011312#endif
11313#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000011314 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000011315#endif
11316#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000011317 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000011318#endif
11319#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000011320 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000011321#endif
11322#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011323 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011324#endif
11325#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000011326 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000011327#endif
11328#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011329 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000011330#endif
11331#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011332 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011333#endif
11334#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011335 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000011336#endif
11337#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011338 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011339#endif
11340#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000011341 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000011342#endif
11343#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011344 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011345#endif
11346#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000011347 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000011348#endif
11349#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011350 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011351#endif
11352#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011353 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000011354#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000011355#ifdef _PC_ACL_ENABLED
11356 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
11357#endif
11358#ifdef _PC_MIN_HOLE_SIZE
11359 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
11360#endif
11361#ifdef _PC_ALLOC_SIZE_MIN
11362 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
11363#endif
11364#ifdef _PC_REC_INCR_XFER_SIZE
11365 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
11366#endif
11367#ifdef _PC_REC_MAX_XFER_SIZE
11368 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
11369#endif
11370#ifdef _PC_REC_MIN_XFER_SIZE
11371 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
11372#endif
11373#ifdef _PC_REC_XFER_ALIGN
11374 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
11375#endif
11376#ifdef _PC_SYMLINK_MAX
11377 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
11378#endif
11379#ifdef _PC_XATTR_ENABLED
11380 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
11381#endif
11382#ifdef _PC_XATTR_EXISTS
11383 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
11384#endif
11385#ifdef _PC_TIMESTAMP_RESOLUTION
11386 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
11387#endif
Fred Drakec9680921999-12-13 16:37:25 +000011388};
11389
Fred Drakec9680921999-12-13 16:37:25 +000011390static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011391conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011392{
11393 return conv_confname(arg, valuep, posix_constants_pathconf,
11394 sizeof(posix_constants_pathconf)
11395 / sizeof(struct constdef));
11396}
11397#endif
11398
Larry Hastings2f936352014-08-05 14:04:04 +100011399
Fred Drakec9680921999-12-13 16:37:25 +000011400#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011401/*[clinic input]
11402os.fpathconf -> long
11403
Gregory P. Smith3ccb96c2020-06-20 15:06:48 -070011404 fd: fildes
Larry Hastings2f936352014-08-05 14:04:04 +100011405 name: path_confname
11406 /
11407
11408Return the configuration limit name for the file descriptor fd.
11409
11410If there is no limit, return -1.
11411[clinic start generated code]*/
11412
Larry Hastings2f936352014-08-05 14:04:04 +100011413static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011414os_fpathconf_impl(PyObject *module, int fd, int name)
Gregory P. Smith3ccb96c2020-06-20 15:06:48 -070011415/*[clinic end generated code: output=d5b7042425fc3e21 input=5b8d2471cfaae186]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011416{
11417 long limit;
11418
11419 errno = 0;
11420 limit = fpathconf(fd, name);
11421 if (limit == -1 && errno != 0)
11422 posix_error();
11423
11424 return limit;
11425}
11426#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011427
11428
11429#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011430/*[clinic input]
11431os.pathconf -> long
11432 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
11433 name: path_confname
11434
11435Return the configuration limit name for the file or directory path.
11436
11437If there is no limit, return -1.
11438On some platforms, path may also be specified as an open file descriptor.
11439 If this functionality is unavailable, using it raises an exception.
11440[clinic start generated code]*/
11441
Larry Hastings2f936352014-08-05 14:04:04 +100011442static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011443os_pathconf_impl(PyObject *module, path_t *path, int name)
11444/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011445{
Victor Stinner8c62be82010-05-06 00:08:46 +000011446 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000011447
Victor Stinner8c62be82010-05-06 00:08:46 +000011448 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020011449#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011450 if (path->fd != -1)
11451 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020011452 else
11453#endif
Larry Hastings2f936352014-08-05 14:04:04 +100011454 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000011455 if (limit == -1 && errno != 0) {
11456 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000011457 /* could be a path or name problem */
11458 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000011459 else
Larry Hastings2f936352014-08-05 14:04:04 +100011460 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000011461 }
Larry Hastings2f936352014-08-05 14:04:04 +100011462
11463 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000011464}
Larry Hastings2f936352014-08-05 14:04:04 +100011465#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011466
11467#ifdef HAVE_CONFSTR
11468static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000011469#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000011470 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000011471#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000011472#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011473 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011474#endif
11475#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011476 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011477#endif
Fred Draked86ed291999-12-15 15:34:33 +000011478#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011479 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011480#endif
11481#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011482 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011483#endif
11484#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011485 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011486#endif
11487#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011488 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011489#endif
Fred Drakec9680921999-12-13 16:37:25 +000011490#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011491 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011492#endif
11493#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011494 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011495#endif
11496#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011497 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011498#endif
11499#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011500 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011501#endif
11502#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011503 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011504#endif
11505#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011506 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011507#endif
11508#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011509 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011510#endif
11511#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011512 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011513#endif
Fred Draked86ed291999-12-15 15:34:33 +000011514#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000011515 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000011516#endif
Fred Drakec9680921999-12-13 16:37:25 +000011517#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000011518 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000011519#endif
Fred Draked86ed291999-12-15 15:34:33 +000011520#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011521 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000011522#endif
11523#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011524 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000011525#endif
11526#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011527 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011528#endif
11529#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011530 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000011531#endif
Fred Drakec9680921999-12-13 16:37:25 +000011532#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011533 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011534#endif
11535#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011536 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011537#endif
11538#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011539 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011540#endif
11541#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011542 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011543#endif
11544#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011545 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011546#endif
11547#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011548 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011549#endif
11550#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011551 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011552#endif
11553#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011554 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011555#endif
11556#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011557 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011558#endif
11559#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011560 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011561#endif
11562#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011563 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011564#endif
11565#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011566 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011567#endif
11568#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011569 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011570#endif
11571#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011572 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011573#endif
11574#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011575 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011576#endif
11577#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011578 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011579#endif
Fred Draked86ed291999-12-15 15:34:33 +000011580#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011581 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011582#endif
11583#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011584 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000011585#endif
11586#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000011587 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000011588#endif
11589#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011590 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011591#endif
11592#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011593 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011594#endif
11595#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000011596 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000011597#endif
11598#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011599 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000011600#endif
11601#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000011602 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000011603#endif
11604#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011605 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011606#endif
11607#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011608 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011609#endif
11610#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011611 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011612#endif
11613#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011614 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011615#endif
11616#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000011617 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000011618#endif
Fred Drakec9680921999-12-13 16:37:25 +000011619};
11620
11621static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011622conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011623{
11624 return conv_confname(arg, valuep, posix_constants_confstr,
11625 sizeof(posix_constants_confstr)
11626 / sizeof(struct constdef));
11627}
11628
Larry Hastings2f936352014-08-05 14:04:04 +100011629
11630/*[clinic input]
11631os.confstr
11632
11633 name: confstr_confname
11634 /
11635
11636Return a string-valued system configuration variable.
11637[clinic start generated code]*/
11638
Larry Hastings2f936352014-08-05 14:04:04 +100011639static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011640os_confstr_impl(PyObject *module, int name)
11641/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000011642{
11643 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000011644 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011645 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000011646
Victor Stinnercb043522010-09-10 23:49:04 +000011647 errno = 0;
11648 len = confstr(name, buffer, sizeof(buffer));
11649 if (len == 0) {
11650 if (errno) {
11651 posix_error();
11652 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000011653 }
11654 else {
Victor Stinnercb043522010-09-10 23:49:04 +000011655 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000011656 }
11657 }
Victor Stinnercb043522010-09-10 23:49:04 +000011658
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011659 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011660 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011661 char *buf = PyMem_Malloc(len);
11662 if (buf == NULL)
11663 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011664 len2 = confstr(name, buf, len);
11665 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011666 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011667 PyMem_Free(buf);
11668 }
11669 else
11670 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011671 return result;
11672}
Larry Hastings2f936352014-08-05 14:04:04 +100011673#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011674
11675
11676#ifdef HAVE_SYSCONF
11677static struct constdef posix_constants_sysconf[] = {
11678#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011679 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011680#endif
11681#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011682 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011683#endif
11684#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011685 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011686#endif
11687#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011688 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011689#endif
11690#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011691 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011692#endif
11693#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011694 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011695#endif
11696#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011697 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011698#endif
11699#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011700 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011701#endif
11702#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011703 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011704#endif
11705#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011706 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011707#endif
Fred Draked86ed291999-12-15 15:34:33 +000011708#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011709 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011710#endif
11711#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011712 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011713#endif
Fred Drakec9680921999-12-13 16:37:25 +000011714#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011715 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011716#endif
Fred Drakec9680921999-12-13 16:37:25 +000011717#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011718 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011719#endif
11720#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011721 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011722#endif
11723#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011724 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011725#endif
11726#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011727 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011728#endif
11729#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011730 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011731#endif
Fred Draked86ed291999-12-15 15:34:33 +000011732#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011733 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011734#endif
Fred Drakec9680921999-12-13 16:37:25 +000011735#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011736 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011737#endif
11738#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011739 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011740#endif
11741#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011742 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011743#endif
11744#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011745 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011746#endif
11747#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011748 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011749#endif
Fred Draked86ed291999-12-15 15:34:33 +000011750#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011751 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011752#endif
Fred Drakec9680921999-12-13 16:37:25 +000011753#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011754 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011755#endif
11756#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011757 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011758#endif
11759#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011760 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011761#endif
11762#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011763 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011764#endif
11765#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011766 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011767#endif
11768#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011769 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011770#endif
11771#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011772 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011773#endif
11774#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011775 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011776#endif
11777#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011778 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011779#endif
11780#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011781 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011782#endif
11783#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011784 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011785#endif
11786#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011787 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011788#endif
11789#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011790 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011791#endif
11792#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011793 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011794#endif
11795#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011796 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011797#endif
11798#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011799 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011800#endif
11801#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011802 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011803#endif
11804#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011805 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011806#endif
11807#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011808 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011809#endif
11810#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011811 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011812#endif
11813#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011814 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011815#endif
11816#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011817 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011818#endif
11819#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011820 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011821#endif
Fred Draked86ed291999-12-15 15:34:33 +000011822#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011823 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011824#endif
Fred Drakec9680921999-12-13 16:37:25 +000011825#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011826 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011827#endif
11828#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011829 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011830#endif
11831#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011832 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011833#endif
Fred Draked86ed291999-12-15 15:34:33 +000011834#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011835 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011836#endif
Fred Drakec9680921999-12-13 16:37:25 +000011837#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011838 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011839#endif
Fred Draked86ed291999-12-15 15:34:33 +000011840#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011841 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011842#endif
11843#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011844 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011845#endif
Fred Drakec9680921999-12-13 16:37:25 +000011846#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011847 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011848#endif
11849#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011850 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011851#endif
11852#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011853 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011854#endif
11855#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011856 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011857#endif
Fred Draked86ed291999-12-15 15:34:33 +000011858#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011859 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011860#endif
Fred Drakec9680921999-12-13 16:37:25 +000011861#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011862 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011863#endif
11864#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011865 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011866#endif
11867#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011868 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011869#endif
11870#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011871 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011872#endif
11873#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011874 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011875#endif
11876#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011877 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011878#endif
11879#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011880 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011881#endif
Fred Draked86ed291999-12-15 15:34:33 +000011882#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011883 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011884#endif
Fred Drakec9680921999-12-13 16:37:25 +000011885#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011886 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011887#endif
11888#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011889 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011890#endif
Fred Draked86ed291999-12-15 15:34:33 +000011891#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011892 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011893#endif
Fred Drakec9680921999-12-13 16:37:25 +000011894#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011895 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011896#endif
11897#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011898 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011899#endif
11900#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011901 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011902#endif
11903#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011904 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011905#endif
11906#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011907 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011908#endif
11909#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011910 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011911#endif
11912#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011913 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011914#endif
11915#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011916 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011917#endif
11918#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011919 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011920#endif
Fred Draked86ed291999-12-15 15:34:33 +000011921#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011922 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011923#endif
11924#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011925 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011926#endif
Fred Drakec9680921999-12-13 16:37:25 +000011927#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011928 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011929#endif
11930#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011931 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011932#endif
11933#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011934 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011935#endif
11936#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011937 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011938#endif
Batuhan Taşkaya909f4a32020-04-05 03:40:49 +030011939#ifdef _SC_AIX_REALMEM
11940 {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
11941#endif
Fred Drakec9680921999-12-13 16:37:25 +000011942#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011943 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011944#endif
11945#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011946 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011947#endif
11948#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011949 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011950#endif
11951#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011952 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011953#endif
11954#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011955 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011956#endif
11957#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011958 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011959#endif
11960#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011961 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011962#endif
11963#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011964 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011965#endif
11966#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011967 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011968#endif
11969#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011970 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011971#endif
11972#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011973 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011974#endif
11975#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011976 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011977#endif
11978#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011979 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011980#endif
11981#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011982 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011983#endif
11984#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011985 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011986#endif
11987#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011988 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011989#endif
11990#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011991 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011992#endif
11993#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011994 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011995#endif
11996#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011997 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011998#endif
11999#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012000 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012001#endif
12002#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012003 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012004#endif
12005#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012006 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000012007#endif
12008#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000012009 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000012010#endif
12011#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012012 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012013#endif
12014#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012015 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012016#endif
12017#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000012018 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000012019#endif
12020#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012021 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012022#endif
12023#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012024 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012025#endif
12026#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012027 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012028#endif
12029#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012030 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012031#endif
12032#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012033 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012034#endif
Fred Draked86ed291999-12-15 15:34:33 +000012035#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000012036 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000012037#endif
Fred Drakec9680921999-12-13 16:37:25 +000012038#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000012039 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000012040#endif
12041#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012042 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012043#endif
12044#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000012045 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000012046#endif
12047#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012048 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012049#endif
12050#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000012051 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000012052#endif
12053#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000012054 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000012055#endif
12056#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000012057 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000012058#endif
12059#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000012060 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000012061#endif
12062#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000012063 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000012064#endif
12065#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012066 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012067#endif
12068#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000012069 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000012070#endif
12071#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012072 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000012073#endif
12074#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012075 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000012076#endif
12077#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000012078 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000012079#endif
12080#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000012081 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000012082#endif
12083#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012084 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012085#endif
12086#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012087 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012088#endif
12089#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000012090 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000012091#endif
12092#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012093 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012094#endif
12095#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012096 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012097#endif
12098#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012099 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012100#endif
12101#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012102 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012103#endif
12104#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012105 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012106#endif
12107#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012108 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012109#endif
12110#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000012111 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000012112#endif
12113#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012114 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012115#endif
12116#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012117 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012118#endif
12119#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012120 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012121#endif
12122#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012123 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000012124#endif
12125#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000012126 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000012127#endif
12128#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000012129 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000012130#endif
12131#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000012132 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000012133#endif
12134#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000012135 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000012136#endif
12137#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000012138 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000012139#endif
12140#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000012141 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000012142#endif
12143#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000012144 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000012145#endif
12146#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000012147 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000012148#endif
12149#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000012150 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000012151#endif
12152#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000012153 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000012154#endif
12155#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000012156 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000012157#endif
12158#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012159 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012160#endif
12161#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012162 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012163#endif
12164#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000012165 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000012166#endif
12167#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000012168 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000012169#endif
12170#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000012171 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000012172#endif
12173};
12174
12175static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012176conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000012177{
12178 return conv_confname(arg, valuep, posix_constants_sysconf,
12179 sizeof(posix_constants_sysconf)
12180 / sizeof(struct constdef));
12181}
12182
Larry Hastings2f936352014-08-05 14:04:04 +100012183
12184/*[clinic input]
12185os.sysconf -> long
12186 name: sysconf_confname
12187 /
12188
12189Return an integer-valued system configuration variable.
12190[clinic start generated code]*/
12191
Larry Hastings2f936352014-08-05 14:04:04 +100012192static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012193os_sysconf_impl(PyObject *module, int name)
12194/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012195{
12196 long value;
12197
12198 errno = 0;
12199 value = sysconf(name);
12200 if (value == -1 && errno != 0)
12201 posix_error();
12202 return value;
12203}
12204#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000012205
12206
Fred Drakebec628d1999-12-15 18:31:10 +000012207/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020012208 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000012209 * the exported dictionaries that are used to publish information about the
12210 * names available on the host platform.
12211 *
12212 * Sorting the table at runtime ensures that the table is properly ordered
12213 * when used, even for platforms we're not able to test on. It also makes
12214 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000012215 */
Fred Drakebec628d1999-12-15 18:31:10 +000012216
12217static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012218cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000012219{
12220 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000012221 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000012222 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000012223 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000012224
12225 return strcmp(c1->name, c2->name);
12226}
12227
12228static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012229setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012230 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000012231{
Fred Drakebec628d1999-12-15 18:31:10 +000012232 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000012233 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000012234
12235 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
12236 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000012237 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000012238 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012239
Barry Warsaw3155db32000-04-13 15:20:40 +000012240 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000012241 PyObject *o = PyLong_FromLong(table[i].value);
12242 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
12243 Py_XDECREF(o);
12244 Py_DECREF(d);
12245 return -1;
12246 }
12247 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000012248 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000012249 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000012250}
12251
Fred Drakebec628d1999-12-15 18:31:10 +000012252/* Return -1 on failure, 0 on success. */
12253static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000012254setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000012255{
12256#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000012257 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000012258 sizeof(posix_constants_pathconf)
12259 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012260 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012261 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012262#endif
12263#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000012264 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000012265 sizeof(posix_constants_confstr)
12266 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012267 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012268 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012269#endif
12270#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000012271 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000012272 sizeof(posix_constants_sysconf)
12273 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012274 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012275 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012276#endif
Fred Drakebec628d1999-12-15 18:31:10 +000012277 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000012278}
Fred Draked86ed291999-12-15 15:34:33 +000012279
12280
Larry Hastings2f936352014-08-05 14:04:04 +100012281/*[clinic input]
12282os.abort
12283
12284Abort the interpreter immediately.
12285
12286This function 'dumps core' or otherwise fails in the hardest way possible
12287on the hosting operating system. This function never returns.
12288[clinic start generated code]*/
12289
Larry Hastings2f936352014-08-05 14:04:04 +100012290static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012291os_abort_impl(PyObject *module)
12292/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012293{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012294 abort();
12295 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010012296#ifndef __clang__
12297 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
12298 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
12299 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012300 Py_FatalError("abort() called from Python code didn't abort!");
12301 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010012302#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012303}
Fred Drakebec628d1999-12-15 18:31:10 +000012304
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000012305#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080012306/* Grab ShellExecute dynamically from shell32 */
12307static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012308static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
12309 LPCWSTR, INT);
12310static int
12311check_ShellExecute()
12312{
12313 HINSTANCE hShell32;
12314
12315 /* only recheck */
12316 if (-1 == has_ShellExecute) {
12317 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070012318 /* Security note: this call is not vulnerable to "DLL hijacking".
12319 SHELL32 is part of "KnownDLLs" and so Windows always load
12320 the system SHELL32.DLL, even if there is another SHELL32.DLL
12321 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080012322 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080012323 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080012324 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
12325 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070012326 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012327 } else {
12328 has_ShellExecute = 0;
12329 }
Tony Roberts4860f012019-02-02 18:16:42 +010012330 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080012331 }
12332 return has_ShellExecute;
12333}
12334
12335
Steve Dowercc16be82016-09-08 10:35:16 -070012336/*[clinic input]
12337os.startfile
12338 filepath: path_t
12339 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000012340
Steve Dowercc16be82016-09-08 10:35:16 -070012341Start a file with its associated application.
12342
12343When "operation" is not specified or "open", this acts like
12344double-clicking the file in Explorer, or giving the file name as an
12345argument to the DOS "start" command: the file is opened with whatever
12346application (if any) its extension is associated.
12347When another "operation" is given, it specifies what should be done with
12348the file. A typical operation is "print".
12349
12350startfile returns as soon as the associated application is launched.
12351There is no option to wait for the application to close, and no way
12352to retrieve the application's exit status.
12353
12354The filepath is relative to the current directory. If you want to use
12355an absolute path, make sure the first character is not a slash ("/");
12356the underlying Win32 ShellExecute function doesn't work if it is.
12357[clinic start generated code]*/
12358
12359static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020012360os_startfile_impl(PyObject *module, path_t *filepath,
12361 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030012362/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070012363{
12364 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012365
12366 if(!check_ShellExecute()) {
12367 /* If the OS doesn't have ShellExecute, return a
12368 NotImplementedError. */
12369 return PyErr_Format(PyExc_NotImplementedError,
12370 "startfile not available on this platform");
12371 }
12372
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012373 if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -080012374 return NULL;
12375 }
12376
Victor Stinner8c62be82010-05-06 00:08:46 +000012377 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070012378 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080012379 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000012380 Py_END_ALLOW_THREADS
12381
Victor Stinner8c62be82010-05-06 00:08:46 +000012382 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070012383 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020012384 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000012385 }
Steve Dowercc16be82016-09-08 10:35:16 -070012386 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000012387}
Larry Hastings2f936352014-08-05 14:04:04 +100012388#endif /* MS_WINDOWS */
12389
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012390
Martin v. Löwis438b5342002-12-27 10:16:42 +000012391#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100012392/*[clinic input]
12393os.getloadavg
12394
12395Return average recent system load information.
12396
12397Return the number of processes in the system run queue averaged over
12398the last 1, 5, and 15 minutes as a tuple of three floats.
12399Raises OSError if the load average was unobtainable.
12400[clinic start generated code]*/
12401
Larry Hastings2f936352014-08-05 14:04:04 +100012402static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012403os_getloadavg_impl(PyObject *module)
12404/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000012405{
12406 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000012407 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000012408 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
12409 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000012410 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000012411 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000012412}
Larry Hastings2f936352014-08-05 14:04:04 +100012413#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000012414
Larry Hastings2f936352014-08-05 14:04:04 +100012415
12416/*[clinic input]
12417os.device_encoding
12418 fd: int
12419
12420Return a string describing the encoding of a terminal's file descriptor.
12421
12422The file descriptor must be attached to a terminal.
12423If the device is not a terminal, return None.
12424[clinic start generated code]*/
12425
Larry Hastings2f936352014-08-05 14:04:04 +100012426static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012427os_device_encoding_impl(PyObject *module, int fd)
12428/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012429{
Brett Cannonefb00c02012-02-29 18:31:31 -050012430 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000012431}
12432
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012433
Larry Hastings2f936352014-08-05 14:04:04 +100012434#ifdef HAVE_SETRESUID
12435/*[clinic input]
12436os.setresuid
12437
12438 ruid: uid_t
12439 euid: uid_t
12440 suid: uid_t
12441 /
12442
12443Set the current process's real, effective, and saved user ids.
12444[clinic start generated code]*/
12445
Larry Hastings2f936352014-08-05 14:04:04 +100012446static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012447os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
12448/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012449{
Victor Stinner8c62be82010-05-06 00:08:46 +000012450 if (setresuid(ruid, euid, suid) < 0)
12451 return posix_error();
12452 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012453}
Larry Hastings2f936352014-08-05 14:04:04 +100012454#endif /* HAVE_SETRESUID */
12455
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012456
12457#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012458/*[clinic input]
12459os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012460
Larry Hastings2f936352014-08-05 14:04:04 +100012461 rgid: gid_t
12462 egid: gid_t
12463 sgid: gid_t
12464 /
12465
12466Set the current process's real, effective, and saved group ids.
12467[clinic start generated code]*/
12468
Larry Hastings2f936352014-08-05 14:04:04 +100012469static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012470os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
12471/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012472{
Victor Stinner8c62be82010-05-06 00:08:46 +000012473 if (setresgid(rgid, egid, sgid) < 0)
12474 return posix_error();
12475 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012476}
Larry Hastings2f936352014-08-05 14:04:04 +100012477#endif /* HAVE_SETRESGID */
12478
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012479
12480#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100012481/*[clinic input]
12482os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012483
Larry Hastings2f936352014-08-05 14:04:04 +100012484Return a tuple of the current process's real, effective, and saved user ids.
12485[clinic start generated code]*/
12486
Larry Hastings2f936352014-08-05 14:04:04 +100012487static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012488os_getresuid_impl(PyObject *module)
12489/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012490{
Victor Stinner8c62be82010-05-06 00:08:46 +000012491 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012492 if (getresuid(&ruid, &euid, &suid) < 0)
12493 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012494 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
12495 _PyLong_FromUid(euid),
12496 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012497}
Larry Hastings2f936352014-08-05 14:04:04 +100012498#endif /* HAVE_GETRESUID */
12499
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012500
12501#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012502/*[clinic input]
12503os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012504
Larry Hastings2f936352014-08-05 14:04:04 +100012505Return a tuple of the current process's real, effective, and saved group ids.
12506[clinic start generated code]*/
12507
Larry Hastings2f936352014-08-05 14:04:04 +100012508static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012509os_getresgid_impl(PyObject *module)
12510/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012511{
12512 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012513 if (getresgid(&rgid, &egid, &sgid) < 0)
12514 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012515 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
12516 _PyLong_FromGid(egid),
12517 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012518}
Larry Hastings2f936352014-08-05 14:04:04 +100012519#endif /* HAVE_GETRESGID */
12520
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012521
Benjamin Peterson9428d532011-09-14 11:45:52 -040012522#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100012523/*[clinic input]
12524os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040012525
Larry Hastings2f936352014-08-05 14:04:04 +100012526 path: path_t(allow_fd=True)
12527 attribute: path_t
12528 *
12529 follow_symlinks: bool = True
12530
12531Return the value of extended attribute attribute on path.
12532
BNMetricsb9427072018-11-02 15:20:19 +000012533path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012534If follow_symlinks is False, and the last element of the path is a symbolic
12535 link, getxattr will examine the symbolic link itself instead of the file
12536 the link points to.
12537
12538[clinic start generated code]*/
12539
Larry Hastings2f936352014-08-05 14:04:04 +100012540static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012541os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012542 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012543/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012544{
12545 Py_ssize_t i;
12546 PyObject *buffer = NULL;
12547
12548 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
12549 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012550
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012551 if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
12552 return NULL;
12553 }
12554
Larry Hastings9cf065c2012-06-22 16:30:09 -070012555 for (i = 0; ; i++) {
12556 void *ptr;
12557 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012558 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070012559 Py_ssize_t buffer_size = buffer_sizes[i];
12560 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100012561 path_error(path);
12562 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012563 }
12564 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
12565 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100012566 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012567 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012568
Larry Hastings9cf065c2012-06-22 16:30:09 -070012569 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012570 if (path->fd >= 0)
12571 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012572 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012573 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012574 else
Larry Hastings2f936352014-08-05 14:04:04 +100012575 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012576 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012577
Larry Hastings9cf065c2012-06-22 16:30:09 -070012578 if (result < 0) {
12579 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012580 if (errno == ERANGE)
12581 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100012582 path_error(path);
12583 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012584 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012585
Larry Hastings9cf065c2012-06-22 16:30:09 -070012586 if (result != buffer_size) {
12587 /* Can only shrink. */
12588 _PyBytes_Resize(&buffer, result);
12589 }
12590 break;
12591 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012592
Larry Hastings9cf065c2012-06-22 16:30:09 -070012593 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012594}
12595
Larry Hastings2f936352014-08-05 14:04:04 +100012596
12597/*[clinic input]
12598os.setxattr
12599
12600 path: path_t(allow_fd=True)
12601 attribute: path_t
12602 value: Py_buffer
12603 flags: int = 0
12604 *
12605 follow_symlinks: bool = True
12606
12607Set extended attribute attribute on path to value.
12608
BNMetricsb9427072018-11-02 15:20:19 +000012609path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012610If follow_symlinks is False, and the last element of the path is a symbolic
12611 link, setxattr will modify the symbolic link itself instead of the file
12612 the link points to.
12613
12614[clinic start generated code]*/
12615
Benjamin Peterson799bd802011-08-31 22:15:17 -040012616static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012617os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012618 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012619/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040012620{
Larry Hastings2f936352014-08-05 14:04:04 +100012621 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012622
Larry Hastings2f936352014-08-05 14:04:04 +100012623 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040012624 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012625
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012626 if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
12627 value->buf, value->len, flags) < 0) {
12628 return NULL;
12629 }
12630
Benjamin Peterson799bd802011-08-31 22:15:17 -040012631 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012632 if (path->fd > -1)
12633 result = fsetxattr(path->fd, attribute->narrow,
12634 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012635 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012636 result = setxattr(path->narrow, attribute->narrow,
12637 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012638 else
Larry Hastings2f936352014-08-05 14:04:04 +100012639 result = lsetxattr(path->narrow, attribute->narrow,
12640 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012641 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012642
Larry Hastings9cf065c2012-06-22 16:30:09 -070012643 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100012644 path_error(path);
12645 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012646 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012647
Larry Hastings2f936352014-08-05 14:04:04 +100012648 Py_RETURN_NONE;
12649}
12650
12651
12652/*[clinic input]
12653os.removexattr
12654
12655 path: path_t(allow_fd=True)
12656 attribute: path_t
12657 *
12658 follow_symlinks: bool = True
12659
12660Remove extended attribute attribute on path.
12661
BNMetricsb9427072018-11-02 15:20:19 +000012662path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012663If follow_symlinks is False, and the last element of the path is a symbolic
12664 link, removexattr will modify the symbolic link itself instead of the file
12665 the link points to.
12666
12667[clinic start generated code]*/
12668
Larry Hastings2f936352014-08-05 14:04:04 +100012669static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012670os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012671 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012672/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012673{
12674 ssize_t result;
12675
12676 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12677 return NULL;
12678
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012679 if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
12680 return NULL;
12681 }
12682
Larry Hastings2f936352014-08-05 14:04:04 +100012683 Py_BEGIN_ALLOW_THREADS;
12684 if (path->fd > -1)
12685 result = fremovexattr(path->fd, attribute->narrow);
12686 else if (follow_symlinks)
12687 result = removexattr(path->narrow, attribute->narrow);
12688 else
12689 result = lremovexattr(path->narrow, attribute->narrow);
12690 Py_END_ALLOW_THREADS;
12691
12692 if (result) {
12693 return path_error(path);
12694 }
12695
12696 Py_RETURN_NONE;
12697}
12698
12699
12700/*[clinic input]
12701os.listxattr
12702
12703 path: path_t(allow_fd=True, nullable=True) = None
12704 *
12705 follow_symlinks: bool = True
12706
12707Return a list of extended attributes on path.
12708
BNMetricsb9427072018-11-02 15:20:19 +000012709path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012710if path is None, listxattr will examine the current directory.
12711If follow_symlinks is False, and the last element of the path is a symbolic
12712 link, listxattr will examine the symbolic link itself instead of the file
12713 the link points to.
12714[clinic start generated code]*/
12715
Larry Hastings2f936352014-08-05 14:04:04 +100012716static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012717os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012718/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012719{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012720 Py_ssize_t i;
12721 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012722 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012723 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012724
Larry Hastings2f936352014-08-05 14:04:04 +100012725 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012726 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012727
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012728 if (PySys_Audit("os.listxattr", "(O)",
12729 path->object ? path->object : Py_None) < 0) {
12730 return NULL;
12731 }
12732
Larry Hastings2f936352014-08-05 14:04:04 +100012733 name = path->narrow ? path->narrow : ".";
12734
Larry Hastings9cf065c2012-06-22 16:30:09 -070012735 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012736 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012737 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012738 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012739 Py_ssize_t buffer_size = buffer_sizes[i];
12740 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012741 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012742 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012743 break;
12744 }
12745 buffer = PyMem_MALLOC(buffer_size);
12746 if (!buffer) {
12747 PyErr_NoMemory();
12748 break;
12749 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012750
Larry Hastings9cf065c2012-06-22 16:30:09 -070012751 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012752 if (path->fd > -1)
12753 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012754 else if (follow_symlinks)
12755 length = listxattr(name, buffer, buffer_size);
12756 else
12757 length = llistxattr(name, buffer, buffer_size);
12758 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012759
Larry Hastings9cf065c2012-06-22 16:30:09 -070012760 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012761 if (errno == ERANGE) {
12762 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012763 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012764 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012765 }
Larry Hastings2f936352014-08-05 14:04:04 +100012766 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012767 break;
12768 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012769
Larry Hastings9cf065c2012-06-22 16:30:09 -070012770 result = PyList_New(0);
12771 if (!result) {
12772 goto exit;
12773 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012774
Larry Hastings9cf065c2012-06-22 16:30:09 -070012775 end = buffer + length;
12776 for (trace = start = buffer; trace != end; trace++) {
12777 if (!*trace) {
12778 int error;
12779 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12780 trace - start);
12781 if (!attribute) {
12782 Py_DECREF(result);
12783 result = NULL;
12784 goto exit;
12785 }
12786 error = PyList_Append(result, attribute);
12787 Py_DECREF(attribute);
12788 if (error) {
12789 Py_DECREF(result);
12790 result = NULL;
12791 goto exit;
12792 }
12793 start = trace + 1;
12794 }
12795 }
12796 break;
12797 }
12798exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012799 if (buffer)
12800 PyMem_FREE(buffer);
12801 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012802}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012803#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012804
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012805
Larry Hastings2f936352014-08-05 14:04:04 +100012806/*[clinic input]
12807os.urandom
12808
12809 size: Py_ssize_t
12810 /
12811
12812Return a bytes object containing random bytes suitable for cryptographic use.
12813[clinic start generated code]*/
12814
Larry Hastings2f936352014-08-05 14:04:04 +100012815static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012816os_urandom_impl(PyObject *module, Py_ssize_t size)
12817/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012818{
12819 PyObject *bytes;
12820 int result;
12821
Georg Brandl2fb477c2012-02-21 00:33:36 +010012822 if (size < 0)
12823 return PyErr_Format(PyExc_ValueError,
12824 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012825 bytes = PyBytes_FromStringAndSize(NULL, size);
12826 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012827 return NULL;
12828
Victor Stinnere66987e2016-09-06 16:33:52 -070012829 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012830 if (result == -1) {
12831 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012832 return NULL;
12833 }
Larry Hastings2f936352014-08-05 14:04:04 +100012834 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012835}
12836
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012837#ifdef HAVE_MEMFD_CREATE
12838/*[clinic input]
12839os.memfd_create
12840
12841 name: FSConverter
12842 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12843
12844[clinic start generated code]*/
12845
12846static PyObject *
12847os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12848/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12849{
12850 int fd;
12851 const char *bytes = PyBytes_AS_STRING(name);
12852 Py_BEGIN_ALLOW_THREADS
12853 fd = memfd_create(bytes, flags);
12854 Py_END_ALLOW_THREADS
12855 if (fd == -1) {
12856 return PyErr_SetFromErrno(PyExc_OSError);
12857 }
12858 return PyLong_FromLong(fd);
12859}
12860#endif
12861
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012862/* Terminal size querying */
12863
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012864PyDoc_STRVAR(TerminalSize_docstring,
12865 "A tuple of (columns, lines) for holding terminal window size");
12866
12867static PyStructSequence_Field TerminalSize_fields[] = {
12868 {"columns", "width of the terminal window in characters"},
12869 {"lines", "height of the terminal window in characters"},
12870 {NULL, NULL}
12871};
12872
12873static PyStructSequence_Desc TerminalSize_desc = {
12874 "os.terminal_size",
12875 TerminalSize_docstring,
12876 TerminalSize_fields,
12877 2,
12878};
12879
12880#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012881/*[clinic input]
12882os.get_terminal_size
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012883
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012884 fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1
12885 /
12886
12887Return the size of the terminal window as (columns, lines).
12888
12889The optional argument fd (default standard output) specifies
12890which file descriptor should be queried.
12891
12892If the file descriptor is not connected to a terminal, an OSError
12893is thrown.
12894
12895This function will only be defined if an implementation is
12896available for this system.
12897
12898shutil.get_terminal_size is the high-level function which should
12899normally be used, os.get_terminal_size is the low-level implementation.
12900[clinic start generated code]*/
12901
12902static PyObject *
12903os_get_terminal_size_impl(PyObject *module, int fd)
12904/*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012905{
12906 int columns, lines;
12907 PyObject *termsize;
12908
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012909 /* Under some conditions stdout may not be connected and
12910 * fileno(stdout) may point to an invalid file descriptor. For example
12911 * GUI apps don't have valid standard streams by default.
12912 *
12913 * If this happens, and the optional fd argument is not present,
12914 * the ioctl below will fail returning EBADF. This is what we want.
12915 */
12916
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012917#ifdef TERMSIZE_USE_IOCTL
12918 {
12919 struct winsize w;
12920 if (ioctl(fd, TIOCGWINSZ, &w))
12921 return PyErr_SetFromErrno(PyExc_OSError);
12922 columns = w.ws_col;
12923 lines = w.ws_row;
12924 }
12925#endif /* TERMSIZE_USE_IOCTL */
12926
12927#ifdef TERMSIZE_USE_CONIO
12928 {
12929 DWORD nhandle;
12930 HANDLE handle;
12931 CONSOLE_SCREEN_BUFFER_INFO csbi;
12932 switch (fd) {
12933 case 0: nhandle = STD_INPUT_HANDLE;
12934 break;
12935 case 1: nhandle = STD_OUTPUT_HANDLE;
12936 break;
12937 case 2: nhandle = STD_ERROR_HANDLE;
12938 break;
12939 default:
12940 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12941 }
12942 handle = GetStdHandle(nhandle);
12943 if (handle == NULL)
12944 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12945 if (handle == INVALID_HANDLE_VALUE)
12946 return PyErr_SetFromWindowsErr(0);
12947
12948 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12949 return PyErr_SetFromWindowsErr(0);
12950
12951 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12952 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12953 }
12954#endif /* TERMSIZE_USE_CONIO */
12955
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012956 PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080012957 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012958 if (termsize == NULL)
12959 return NULL;
12960 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12961 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12962 if (PyErr_Occurred()) {
12963 Py_DECREF(termsize);
12964 return NULL;
12965 }
12966 return termsize;
12967}
12968#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12969
Larry Hastings2f936352014-08-05 14:04:04 +100012970
12971/*[clinic input]
12972os.cpu_count
12973
Charles-François Natali80d62e62015-08-13 20:37:08 +010012974Return the number of CPUs in the system; return None if indeterminable.
12975
12976This number is not equivalent to the number of CPUs the current process can
12977use. The number of usable CPUs can be obtained with
12978``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012979[clinic start generated code]*/
12980
Larry Hastings2f936352014-08-05 14:04:04 +100012981static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012982os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012983/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012984{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012985 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012986#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012987 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012988#elif defined(__hpux)
12989 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12990#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12991 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
pxinwr3405e052020-08-07 13:21:52 +080012992#elif defined(__VXWORKS__)
12993 ncpu = _Py_popcount32(vxCpuEnabledGet());
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012994#elif defined(__DragonFly__) || \
12995 defined(__OpenBSD__) || \
12996 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012997 defined(__NetBSD__) || \
12998 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012999 int mib[2];
13000 size_t len = sizeof(ncpu);
13001 mib[0] = CTL_HW;
13002 mib[1] = HW_NCPU;
13003 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
13004 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020013005#endif
13006 if (ncpu >= 1)
13007 return PyLong_FromLong(ncpu);
13008 else
13009 Py_RETURN_NONE;
13010}
13011
Victor Stinnerdaf45552013-08-28 00:53:59 +020013012
Larry Hastings2f936352014-08-05 14:04:04 +100013013/*[clinic input]
13014os.get_inheritable -> bool
13015
13016 fd: int
13017 /
13018
13019Get the close-on-exe flag of the specified file descriptor.
13020[clinic start generated code]*/
13021
Larry Hastings2f936352014-08-05 14:04:04 +100013022static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030013023os_get_inheritable_impl(PyObject *module, int fd)
13024/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013025{
Steve Dower8fc89802015-04-12 00:26:27 -040013026 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040013027 _Py_BEGIN_SUPPRESS_IPH
13028 return_value = _Py_get_inheritable(fd);
13029 _Py_END_SUPPRESS_IPH
13030 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100013031}
13032
13033
13034/*[clinic input]
13035os.set_inheritable
13036 fd: int
13037 inheritable: int
13038 /
13039
13040Set the inheritable flag of the specified file descriptor.
13041[clinic start generated code]*/
13042
Larry Hastings2f936352014-08-05 14:04:04 +100013043static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030013044os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
13045/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020013046{
Steve Dower8fc89802015-04-12 00:26:27 -040013047 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013048
Steve Dower8fc89802015-04-12 00:26:27 -040013049 _Py_BEGIN_SUPPRESS_IPH
13050 result = _Py_set_inheritable(fd, inheritable, NULL);
13051 _Py_END_SUPPRESS_IPH
13052 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020013053 return NULL;
13054 Py_RETURN_NONE;
13055}
13056
13057
13058#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100013059/*[clinic input]
13060os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070013061 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100013062 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020013063
Larry Hastings2f936352014-08-05 14:04:04 +100013064Get the close-on-exe flag of the specified file descriptor.
13065[clinic start generated code]*/
13066
Larry Hastings2f936352014-08-05 14:04:04 +100013067static int
Benjamin Petersonca470632016-09-06 13:47:26 -070013068os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070013069/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013070{
13071 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013072
13073 if (!GetHandleInformation((HANDLE)handle, &flags)) {
13074 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100013075 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013076 }
13077
Larry Hastings2f936352014-08-05 14:04:04 +100013078 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013079}
13080
Victor Stinnerdaf45552013-08-28 00:53:59 +020013081
Larry Hastings2f936352014-08-05 14:04:04 +100013082/*[clinic input]
13083os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070013084 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100013085 inheritable: bool
13086 /
13087
13088Set the inheritable flag of the specified handle.
13089[clinic start generated code]*/
13090
Larry Hastings2f936352014-08-05 14:04:04 +100013091static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070013092os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040013093 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070013094/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013095{
13096 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013097 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
13098 PyErr_SetFromWindowsErr(0);
13099 return NULL;
13100 }
13101 Py_RETURN_NONE;
13102}
Larry Hastings2f936352014-08-05 14:04:04 +100013103#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013104
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013105#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013106/*[clinic input]
13107os.get_blocking -> bool
13108 fd: int
13109 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013110
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013111Get the blocking mode of the file descriptor.
13112
13113Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
13114[clinic start generated code]*/
13115
13116static int
13117os_get_blocking_impl(PyObject *module, int fd)
13118/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013119{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013120 int blocking;
13121
Steve Dower8fc89802015-04-12 00:26:27 -040013122 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013123 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040013124 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013125 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013126}
13127
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013128/*[clinic input]
13129os.set_blocking
13130 fd: int
13131 blocking: bool(accept={int})
13132 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013133
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013134Set the blocking mode of the specified file descriptor.
13135
13136Set the O_NONBLOCK flag if blocking is False,
13137clear the O_NONBLOCK flag otherwise.
13138[clinic start generated code]*/
13139
13140static PyObject *
13141os_set_blocking_impl(PyObject *module, int fd, int blocking)
13142/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013143{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013144 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013145
Steve Dower8fc89802015-04-12 00:26:27 -040013146 _Py_BEGIN_SUPPRESS_IPH
13147 result = _Py_set_blocking(fd, blocking);
13148 _Py_END_SUPPRESS_IPH
13149 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013150 return NULL;
13151 Py_RETURN_NONE;
13152}
13153#endif /* !MS_WINDOWS */
13154
13155
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013156/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080013157class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013158[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080013159/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013160
13161typedef struct {
13162 PyObject_HEAD
13163 PyObject *name;
13164 PyObject *path;
13165 PyObject *stat;
13166 PyObject *lstat;
13167#ifdef MS_WINDOWS
13168 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010013169 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010013170 int got_file_index;
13171#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010013172#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013173 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013174#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013175 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013176 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010013177#endif
13178} DirEntry;
13179
Eddie Elizondob3966632019-11-05 07:16:14 -080013180static PyObject *
13181_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
13182{
13183 PyErr_Format(PyExc_TypeError,
13184 "cannot create '%.100s' instances", _PyType_Name(type));
13185 return NULL;
13186}
13187
Victor Stinner6036e442015-03-08 01:58:04 +010013188static void
13189DirEntry_dealloc(DirEntry *entry)
13190{
Eddie Elizondob3966632019-11-05 07:16:14 -080013191 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010013192 Py_XDECREF(entry->name);
13193 Py_XDECREF(entry->path);
13194 Py_XDECREF(entry->stat);
13195 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080013196 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13197 free_func(entry);
13198 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013199}
13200
13201/* Forward reference */
13202static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013203DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
13204 int follow_symlinks, unsigned short mode_bits);
Victor Stinner6036e442015-03-08 01:58:04 +010013205
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013206/*[clinic input]
13207os.DirEntry.is_symlink -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013208 defining_class: defining_class
13209 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013210
13211Return True if the entry is a symbolic link; cached per entry.
13212[clinic start generated code]*/
13213
Victor Stinner6036e442015-03-08 01:58:04 +010013214static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013215os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class)
13216/*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013217{
13218#ifdef MS_WINDOWS
13219 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010013220#elif defined(HAVE_DIRENT_D_TYPE)
13221 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013222 if (self->d_type != DT_UNKNOWN)
13223 return self->d_type == DT_LNK;
13224 else
Victor Stinner97f33c32020-05-14 18:05:58 +020013225 return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010013226#else
13227 /* POSIX without d_type */
Victor Stinner97f33c32020-05-14 18:05:58 +020013228 return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010013229#endif
13230}
13231
13232static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013233DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks)
Victor Stinner6036e442015-03-08 01:58:04 +010013234{
13235 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013236 STRUCT_STAT st;
13237 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010013238
13239#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013240 if (!PyUnicode_FSDecoder(self->path, &ub))
13241 return NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013242#if USE_UNICODE_WCHAR_CACHE
13243_Py_COMP_DIAG_PUSH
13244_Py_COMP_DIAG_IGNORE_DEPR_DECLS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013245 const wchar_t *path = PyUnicode_AsUnicode(ub);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013246_Py_COMP_DIAG_POP
13247#else /* USE_UNICODE_WCHAR_CACHE */
13248 wchar_t *path = PyUnicode_AsWideCharString(ub, NULL);
13249 Py_DECREF(ub);
13250#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013251#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013252 if (!PyUnicode_FSConverter(self->path, &ub))
13253 return NULL;
13254 const char *path = PyBytes_AS_STRING(ub);
13255 if (self->dir_fd != DEFAULT_DIR_FD) {
13256#ifdef HAVE_FSTATAT
Ronald Oussoren41761932020-11-08 10:05:27 +010013257 if (HAVE_FSTATAT_RUNTIME) {
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013258 result = fstatat(self->dir_fd, path, &st,
13259 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
Ronald Oussoren41761932020-11-08 10:05:27 +010013260 } else
13261
13262#endif /* HAVE_FSTATAT */
13263 {
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013264 Py_DECREF(ub);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013265 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
13266 return NULL;
Ronald Oussoren41761932020-11-08 10:05:27 +010013267 }
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013268 }
13269 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013270#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013271 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013272 if (follow_symlinks)
13273 result = STAT(path, &st);
13274 else
13275 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013276 }
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013277#if defined(MS_WINDOWS) && !USE_UNICODE_WCHAR_CACHE
13278 PyMem_Free(path);
13279#else /* USE_UNICODE_WCHAR_CACHE */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013280 Py_DECREF(ub);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013281#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013282
13283 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013284 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013285
Victor Stinner97f33c32020-05-14 18:05:58 +020013286 return _pystat_fromstructstat(module, &st);
Victor Stinner6036e442015-03-08 01:58:04 +010013287}
13288
13289static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013290DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self)
Victor Stinner6036e442015-03-08 01:58:04 +010013291{
13292 if (!self->lstat) {
Victor Stinner97f33c32020-05-14 18:05:58 +020013293 PyObject *module = PyType_GetModule(defining_class);
Victor Stinner6036e442015-03-08 01:58:04 +010013294#ifdef MS_WINDOWS
Victor Stinner97f33c32020-05-14 18:05:58 +020013295 self->lstat = _pystat_fromstructstat(module, &self->win32_lstat);
Victor Stinner6036e442015-03-08 01:58:04 +010013296#else /* POSIX */
Victor Stinner97f33c32020-05-14 18:05:58 +020013297 self->lstat = DirEntry_fetch_stat(module, self, 0);
Victor Stinner6036e442015-03-08 01:58:04 +010013298#endif
13299 }
13300 Py_XINCREF(self->lstat);
13301 return self->lstat;
13302}
13303
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013304/*[clinic input]
13305os.DirEntry.stat
Victor Stinner97f33c32020-05-14 18:05:58 +020013306 defining_class: defining_class
13307 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013308 *
13309 follow_symlinks: bool = True
13310
13311Return stat_result object for the entry; cached per entry.
13312[clinic start generated code]*/
13313
Victor Stinner6036e442015-03-08 01:58:04 +010013314static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013315os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
13316 int follow_symlinks)
13317/*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013318{
Victor Stinner97f33c32020-05-14 18:05:58 +020013319 if (!follow_symlinks) {
13320 return DirEntry_get_lstat(defining_class, self);
13321 }
Victor Stinner6036e442015-03-08 01:58:04 +010013322
13323 if (!self->stat) {
Victor Stinner97f33c32020-05-14 18:05:58 +020013324 int result = os_DirEntry_is_symlink_impl(self, defining_class);
13325 if (result == -1) {
Victor Stinner6036e442015-03-08 01:58:04 +010013326 return NULL;
Victor Stinner97f33c32020-05-14 18:05:58 +020013327 }
13328 if (result) {
13329 PyObject *module = PyType_GetModule(defining_class);
13330 self->stat = DirEntry_fetch_stat(module, self, 1);
13331 }
13332 else {
13333 self->stat = DirEntry_get_lstat(defining_class, self);
13334 }
Victor Stinner6036e442015-03-08 01:58:04 +010013335 }
13336
13337 Py_XINCREF(self->stat);
13338 return self->stat;
13339}
13340
Victor Stinner6036e442015-03-08 01:58:04 +010013341/* Set exception and return -1 on error, 0 for False, 1 for True */
13342static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013343DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
13344 int follow_symlinks, unsigned short mode_bits)
Victor Stinner6036e442015-03-08 01:58:04 +010013345{
13346 PyObject *stat = NULL;
13347 PyObject *st_mode = NULL;
13348 long mode;
13349 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010013350#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013351 int is_symlink;
13352 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010013353#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013354#ifdef MS_WINDOWS
13355 unsigned long dir_bits;
13356#endif
13357
13358#ifdef MS_WINDOWS
13359 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
13360 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010013361#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013362 is_symlink = self->d_type == DT_LNK;
13363 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
13364#endif
13365
Victor Stinner35a97c02015-03-08 02:59:09 +010013366#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013367 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010013368#endif
Victor Stinner97f33c32020-05-14 18:05:58 +020013369 stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010013370 if (!stat) {
13371 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
13372 /* If file doesn't exist (anymore), then return False
13373 (i.e., say it's not a file/directory) */
13374 PyErr_Clear();
13375 return 0;
13376 }
13377 goto error;
13378 }
Victor Stinner97f33c32020-05-14 18:05:58 +020013379 _posixstate* state = get_posix_state(PyType_GetModule(defining_class));
13380 st_mode = PyObject_GetAttr(stat, state->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010013381 if (!st_mode)
13382 goto error;
13383
13384 mode = PyLong_AsLong(st_mode);
13385 if (mode == -1 && PyErr_Occurred())
13386 goto error;
13387 Py_CLEAR(st_mode);
13388 Py_CLEAR(stat);
13389 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010013390#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013391 }
13392 else if (is_symlink) {
13393 assert(mode_bits != S_IFLNK);
13394 result = 0;
13395 }
13396 else {
13397 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
13398#ifdef MS_WINDOWS
13399 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
13400 if (mode_bits == S_IFDIR)
13401 result = dir_bits != 0;
13402 else
13403 result = dir_bits == 0;
13404#else /* POSIX */
13405 if (mode_bits == S_IFDIR)
13406 result = self->d_type == DT_DIR;
13407 else
13408 result = self->d_type == DT_REG;
13409#endif
13410 }
Victor Stinner35a97c02015-03-08 02:59:09 +010013411#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013412
13413 return result;
13414
13415error:
13416 Py_XDECREF(st_mode);
13417 Py_XDECREF(stat);
13418 return -1;
13419}
13420
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013421/*[clinic input]
13422os.DirEntry.is_dir -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013423 defining_class: defining_class
13424 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013425 *
13426 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010013427
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013428Return True if the entry is a directory; cached per entry.
13429[clinic start generated code]*/
13430
13431static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013432os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class,
13433 int follow_symlinks)
13434/*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013435{
Victor Stinner97f33c32020-05-14 18:05:58 +020013436 return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010013437}
13438
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013439/*[clinic input]
13440os.DirEntry.is_file -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013441 defining_class: defining_class
13442 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013443 *
13444 follow_symlinks: bool = True
13445
13446Return True if the entry is a file; cached per entry.
13447[clinic start generated code]*/
13448
13449static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013450os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class,
13451 int follow_symlinks)
13452/*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013453{
Victor Stinner97f33c32020-05-14 18:05:58 +020013454 return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010013455}
13456
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013457/*[clinic input]
13458os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010013459
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013460Return inode of the entry; cached per entry.
13461[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013462
13463static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013464os_DirEntry_inode_impl(DirEntry *self)
13465/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013466{
13467#ifdef MS_WINDOWS
13468 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013469 PyObject *unicode;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013470 STRUCT_STAT stat;
13471 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010013472
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013473 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010013474 return NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013475#if USE_UNICODE_WCHAR_CACHE
13476_Py_COMP_DIAG_PUSH
13477_Py_COMP_DIAG_IGNORE_DEPR_DECLS
13478 const wchar_t *path = PyUnicode_AsUnicode(unicode);
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013479 result = LSTAT(path, &stat);
13480 Py_DECREF(unicode);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013481_Py_COMP_DIAG_POP
13482#else /* USE_UNICODE_WCHAR_CACHE */
13483 wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL);
13484 Py_DECREF(unicode);
13485 result = LSTAT(path, &stat);
13486 PyMem_Free(path);
13487#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013488
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013489 if (result != 0)
13490 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013491
13492 self->win32_file_index = stat.st_ino;
13493 self->got_file_index = 1;
13494 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010013495 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
13496 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010013497#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020013498 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
13499 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010013500#endif
13501}
13502
13503static PyObject *
13504DirEntry_repr(DirEntry *self)
13505{
13506 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
13507}
13508
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013509/*[clinic input]
13510os.DirEntry.__fspath__
13511
13512Returns the path for the entry.
13513[clinic start generated code]*/
13514
Brett Cannon96881cd2016-06-10 14:37:21 -070013515static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013516os_DirEntry___fspath___impl(DirEntry *self)
13517/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070013518{
13519 Py_INCREF(self->path);
13520 return self->path;
13521}
13522
Victor Stinner6036e442015-03-08 01:58:04 +010013523static PyMemberDef DirEntry_members[] = {
13524 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
13525 "the entry's base filename, relative to scandir() \"path\" argument"},
13526 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
13527 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
13528 {NULL}
13529};
13530
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013531#include "clinic/posixmodule.c.h"
13532
Victor Stinner6036e442015-03-08 01:58:04 +010013533static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013534 OS_DIRENTRY_IS_DIR_METHODDEF
13535 OS_DIRENTRY_IS_FILE_METHODDEF
13536 OS_DIRENTRY_IS_SYMLINK_METHODDEF
13537 OS_DIRENTRY_STAT_METHODDEF
13538 OS_DIRENTRY_INODE_METHODDEF
13539 OS_DIRENTRY___FSPATH___METHODDEF
Batuhan Taşkayaf9dd51e2020-04-08 00:37:19 +030013540 {"__class_getitem__", (PyCFunction)Py_GenericAlias,
13541 METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Victor Stinner6036e442015-03-08 01:58:04 +010013542 {NULL}
13543};
13544
Eddie Elizondob3966632019-11-05 07:16:14 -080013545static PyType_Slot DirEntryType_slots[] = {
13546 {Py_tp_new, _disabled_new},
13547 {Py_tp_dealloc, DirEntry_dealloc},
13548 {Py_tp_repr, DirEntry_repr},
13549 {Py_tp_methods, DirEntry_methods},
13550 {Py_tp_members, DirEntry_members},
13551 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010013552};
13553
Eddie Elizondob3966632019-11-05 07:16:14 -080013554static PyType_Spec DirEntryType_spec = {
13555 MODNAME ".DirEntry",
13556 sizeof(DirEntry),
13557 0,
13558 Py_TPFLAGS_DEFAULT,
13559 DirEntryType_slots
13560};
13561
13562
Victor Stinner6036e442015-03-08 01:58:04 +010013563#ifdef MS_WINDOWS
13564
13565static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030013566join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010013567{
13568 Py_ssize_t path_len;
13569 Py_ssize_t size;
13570 wchar_t *result;
13571 wchar_t ch;
13572
13573 if (!path_wide) { /* Default arg: "." */
13574 path_wide = L".";
13575 path_len = 1;
13576 }
13577 else {
13578 path_len = wcslen(path_wide);
13579 }
13580
13581 /* The +1's are for the path separator and the NUL */
13582 size = path_len + 1 + wcslen(filename) + 1;
13583 result = PyMem_New(wchar_t, size);
13584 if (!result) {
13585 PyErr_NoMemory();
13586 return NULL;
13587 }
13588 wcscpy(result, path_wide);
13589 if (path_len > 0) {
13590 ch = result[path_len - 1];
13591 if (ch != SEP && ch != ALTSEP && ch != L':')
13592 result[path_len++] = SEP;
13593 wcscpy(result + path_len, filename);
13594 }
13595 return result;
13596}
13597
13598static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +020013599DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
Victor Stinner6036e442015-03-08 01:58:04 +010013600{
13601 DirEntry *entry;
13602 BY_HANDLE_FILE_INFORMATION file_info;
13603 ULONG reparse_tag;
13604 wchar_t *joined_path;
13605
Victor Stinner1c2fa782020-05-10 11:05:29 +020013606 PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013607 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013608 if (!entry)
13609 return NULL;
13610 entry->name = NULL;
13611 entry->path = NULL;
13612 entry->stat = NULL;
13613 entry->lstat = NULL;
13614 entry->got_file_index = 0;
13615
13616 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
13617 if (!entry->name)
13618 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013619 if (path->narrow) {
13620 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
13621 if (!entry->name)
13622 goto error;
13623 }
Victor Stinner6036e442015-03-08 01:58:04 +010013624
13625 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
13626 if (!joined_path)
13627 goto error;
13628
13629 entry->path = PyUnicode_FromWideChar(joined_path, -1);
13630 PyMem_Free(joined_path);
13631 if (!entry->path)
13632 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013633 if (path->narrow) {
13634 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
13635 if (!entry->path)
13636 goto error;
13637 }
Victor Stinner6036e442015-03-08 01:58:04 +010013638
Steve Dowercc16be82016-09-08 10:35:16 -070013639 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010013640 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
13641
13642 return (PyObject *)entry;
13643
13644error:
13645 Py_DECREF(entry);
13646 return NULL;
13647}
13648
13649#else /* POSIX */
13650
13651static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013652join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010013653{
13654 Py_ssize_t path_len;
13655 Py_ssize_t size;
13656 char *result;
13657
13658 if (!path_narrow) { /* Default arg: "." */
13659 path_narrow = ".";
13660 path_len = 1;
13661 }
13662 else {
13663 path_len = strlen(path_narrow);
13664 }
13665
13666 if (filename_len == -1)
13667 filename_len = strlen(filename);
13668
13669 /* The +1's are for the path separator and the NUL */
13670 size = path_len + 1 + filename_len + 1;
13671 result = PyMem_New(char, size);
13672 if (!result) {
13673 PyErr_NoMemory();
13674 return NULL;
13675 }
13676 strcpy(result, path_narrow);
13677 if (path_len > 0 && result[path_len - 1] != '/')
13678 result[path_len++] = '/';
13679 strcpy(result + path_len, filename);
13680 return result;
13681}
13682
13683static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +020013684DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
13685 Py_ssize_t name_len, ino_t d_ino
Victor Stinner35a97c02015-03-08 02:59:09 +010013686#ifdef HAVE_DIRENT_D_TYPE
13687 , unsigned char d_type
13688#endif
13689 )
Victor Stinner6036e442015-03-08 01:58:04 +010013690{
13691 DirEntry *entry;
13692 char *joined_path;
13693
Victor Stinner1c2fa782020-05-10 11:05:29 +020013694 PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013695 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013696 if (!entry)
13697 return NULL;
13698 entry->name = NULL;
13699 entry->path = NULL;
13700 entry->stat = NULL;
13701 entry->lstat = NULL;
13702
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013703 if (path->fd != -1) {
13704 entry->dir_fd = path->fd;
13705 joined_path = NULL;
13706 }
13707 else {
13708 entry->dir_fd = DEFAULT_DIR_FD;
13709 joined_path = join_path_filename(path->narrow, name, name_len);
13710 if (!joined_path)
13711 goto error;
13712 }
Victor Stinner6036e442015-03-08 01:58:04 +010013713
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030013714 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010013715 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013716 if (joined_path)
13717 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013718 }
13719 else {
13720 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013721 if (joined_path)
13722 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013723 }
13724 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013725 if (!entry->name)
13726 goto error;
13727
13728 if (path->fd != -1) {
13729 entry->path = entry->name;
13730 Py_INCREF(entry->path);
13731 }
13732 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010013733 goto error;
13734
Victor Stinner35a97c02015-03-08 02:59:09 +010013735#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013736 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013737#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013738 entry->d_ino = d_ino;
13739
13740 return (PyObject *)entry;
13741
13742error:
13743 Py_XDECREF(entry);
13744 return NULL;
13745}
13746
13747#endif
13748
13749
13750typedef struct {
13751 PyObject_HEAD
13752 path_t path;
13753#ifdef MS_WINDOWS
13754 HANDLE handle;
13755 WIN32_FIND_DATAW file_data;
13756 int first_time;
13757#else /* POSIX */
13758 DIR *dirp;
13759#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013760#ifdef HAVE_FDOPENDIR
13761 int fd;
13762#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013763} ScandirIterator;
13764
13765#ifdef MS_WINDOWS
13766
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013767static int
13768ScandirIterator_is_closed(ScandirIterator *iterator)
13769{
13770 return iterator->handle == INVALID_HANDLE_VALUE;
13771}
13772
Victor Stinner6036e442015-03-08 01:58:04 +010013773static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013774ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013775{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013776 HANDLE handle = iterator->handle;
13777
13778 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013779 return;
13780
Victor Stinner6036e442015-03-08 01:58:04 +010013781 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013782 Py_BEGIN_ALLOW_THREADS
13783 FindClose(handle);
13784 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013785}
13786
13787static PyObject *
13788ScandirIterator_iternext(ScandirIterator *iterator)
13789{
13790 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13791 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013792 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013793
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013794 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013795 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013796 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013797
13798 while (1) {
13799 if (!iterator->first_time) {
13800 Py_BEGIN_ALLOW_THREADS
13801 success = FindNextFileW(iterator->handle, file_data);
13802 Py_END_ALLOW_THREADS
13803 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013804 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013805 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013806 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013807 break;
13808 }
13809 }
13810 iterator->first_time = 0;
13811
13812 /* Skip over . and .. */
13813 if (wcscmp(file_data->cFileName, L".") != 0 &&
Victor Stinner1c2fa782020-05-10 11:05:29 +020013814 wcscmp(file_data->cFileName, L"..") != 0)
13815 {
13816 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
13817 entry = DirEntry_from_find_data(module, &iterator->path, file_data);
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013818 if (!entry)
13819 break;
13820 return entry;
13821 }
Victor Stinner6036e442015-03-08 01:58:04 +010013822
13823 /* Loop till we get a non-dot directory or finish iterating */
13824 }
13825
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013826 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013827 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013828 return NULL;
13829}
13830
13831#else /* POSIX */
13832
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013833static int
13834ScandirIterator_is_closed(ScandirIterator *iterator)
13835{
13836 return !iterator->dirp;
13837}
13838
Victor Stinner6036e442015-03-08 01:58:04 +010013839static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013840ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013841{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013842 DIR *dirp = iterator->dirp;
13843
13844 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013845 return;
13846
Victor Stinner6036e442015-03-08 01:58:04 +010013847 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013848 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013849#ifdef HAVE_FDOPENDIR
13850 if (iterator->path.fd != -1)
13851 rewinddir(dirp);
13852#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013853 closedir(dirp);
13854 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013855 return;
13856}
13857
13858static PyObject *
13859ScandirIterator_iternext(ScandirIterator *iterator)
13860{
13861 struct dirent *direntp;
13862 Py_ssize_t name_len;
13863 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013864 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013865
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013866 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013867 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013868 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013869
13870 while (1) {
13871 errno = 0;
13872 Py_BEGIN_ALLOW_THREADS
13873 direntp = readdir(iterator->dirp);
13874 Py_END_ALLOW_THREADS
13875
13876 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013877 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013878 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013879 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013880 break;
13881 }
13882
13883 /* Skip over . and .. */
13884 name_len = NAMLEN(direntp);
13885 is_dot = direntp->d_name[0] == '.' &&
13886 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13887 if (!is_dot) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020013888 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
13889 entry = DirEntry_from_posix_info(module,
13890 &iterator->path, direntp->d_name,
13891 name_len, direntp->d_ino
Victor Stinner35a97c02015-03-08 02:59:09 +010013892#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner1c2fa782020-05-10 11:05:29 +020013893 , direntp->d_type
Victor Stinner35a97c02015-03-08 02:59:09 +010013894#endif
13895 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013896 if (!entry)
13897 break;
13898 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013899 }
13900
13901 /* Loop till we get a non-dot directory or finish iterating */
13902 }
13903
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013904 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013905 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013906 return NULL;
13907}
13908
13909#endif
13910
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013911static PyObject *
13912ScandirIterator_close(ScandirIterator *self, PyObject *args)
13913{
13914 ScandirIterator_closedir(self);
13915 Py_RETURN_NONE;
13916}
13917
13918static PyObject *
13919ScandirIterator_enter(PyObject *self, PyObject *args)
13920{
13921 Py_INCREF(self);
13922 return self;
13923}
13924
13925static PyObject *
13926ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13927{
13928 ScandirIterator_closedir(self);
13929 Py_RETURN_NONE;
13930}
13931
Victor Stinner6036e442015-03-08 01:58:04 +010013932static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013933ScandirIterator_finalize(ScandirIterator *iterator)
13934{
13935 PyObject *error_type, *error_value, *error_traceback;
13936
13937 /* Save the current exception, if any. */
13938 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13939
13940 if (!ScandirIterator_is_closed(iterator)) {
13941 ScandirIterator_closedir(iterator);
13942
13943 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13944 "unclosed scandir iterator %R", iterator)) {
13945 /* Spurious errors can appear at shutdown */
13946 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13947 PyErr_WriteUnraisable((PyObject *) iterator);
13948 }
13949 }
13950 }
13951
Victor Stinner7bfa4092016-03-23 00:43:54 +010013952 path_cleanup(&iterator->path);
13953
13954 /* Restore the saved exception. */
13955 PyErr_Restore(error_type, error_value, error_traceback);
13956}
13957
13958static void
Victor Stinner6036e442015-03-08 01:58:04 +010013959ScandirIterator_dealloc(ScandirIterator *iterator)
13960{
Eddie Elizondob3966632019-11-05 07:16:14 -080013961 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010013962 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13963 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013964
Eddie Elizondob3966632019-11-05 07:16:14 -080013965 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13966 free_func(iterator);
13967 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013968}
13969
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013970static PyMethodDef ScandirIterator_methods[] = {
13971 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13972 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13973 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13974 {NULL}
13975};
13976
Eddie Elizondob3966632019-11-05 07:16:14 -080013977static PyType_Slot ScandirIteratorType_slots[] = {
13978 {Py_tp_new, _disabled_new},
13979 {Py_tp_dealloc, ScandirIterator_dealloc},
13980 {Py_tp_finalize, ScandirIterator_finalize},
13981 {Py_tp_iter, PyObject_SelfIter},
13982 {Py_tp_iternext, ScandirIterator_iternext},
13983 {Py_tp_methods, ScandirIterator_methods},
13984 {0, 0},
13985};
13986
13987static PyType_Spec ScandirIteratorType_spec = {
13988 MODNAME ".ScandirIterator",
13989 sizeof(ScandirIterator),
13990 0,
Victor Stinner97f33c32020-05-14 18:05:58 +020013991 // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
13992 // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
Eddie Elizondob3966632019-11-05 07:16:14 -080013993 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
13994 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010013995};
13996
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013997/*[clinic input]
13998os.scandir
13999
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014000 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014001
14002Return an iterator of DirEntry objects for given path.
14003
BNMetricsb9427072018-11-02 15:20:19 +000014004path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014005is bytes, the names of yielded DirEntry objects will also be bytes; in
14006all other circumstances they will be str.
14007
14008If path is None, uses the path='.'.
14009[clinic start generated code]*/
14010
Victor Stinner6036e442015-03-08 01:58:04 +010014011static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014012os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000014013/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010014014{
14015 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010014016#ifdef MS_WINDOWS
14017 wchar_t *path_strW;
14018#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014019 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014020#ifdef HAVE_FDOPENDIR
14021 int fd = -1;
14022#endif
Victor Stinner6036e442015-03-08 01:58:04 +010014023#endif
14024
Steve Dower60419a72019-06-24 08:42:54 -070014025 if (PySys_Audit("os.scandir", "O",
14026 path->object ? path->object : Py_None) < 0) {
14027 return NULL;
14028 }
14029
Hai Shif707d942020-03-16 21:15:01 +080014030 PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014031 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010014032 if (!iterator)
14033 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010014034
14035#ifdef MS_WINDOWS
14036 iterator->handle = INVALID_HANDLE_VALUE;
14037#else
14038 iterator->dirp = NULL;
14039#endif
14040
Serhiy Storchaka095ef732017-02-09 20:05:51 +020014041 /* Move the ownership to iterator->path */
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014042 memcpy(&iterator->path, path, sizeof(path_t));
14043 memset(path, 0, sizeof(path_t));
Victor Stinner6036e442015-03-08 01:58:04 +010014044
14045#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010014046 iterator->first_time = 1;
14047
14048 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
14049 if (!path_strW)
14050 goto error;
14051
14052 Py_BEGIN_ALLOW_THREADS
14053 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
14054 Py_END_ALLOW_THREADS
14055
14056 PyMem_Free(path_strW);
14057
14058 if (iterator->handle == INVALID_HANDLE_VALUE) {
14059 path_error(&iterator->path);
14060 goto error;
14061 }
14062#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010014063 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014064#ifdef HAVE_FDOPENDIR
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014065 if (iterator->path.fd != -1) {
Ronald Oussoren41761932020-11-08 10:05:27 +010014066 if (HAVE_FDOPENDIR_RUNTIME) {
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014067 /* closedir() closes the FD, so we duplicate it */
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014068 fd = _Py_dup(iterator->path.fd);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014069 if (fd == -1)
14070 goto error;
14071
14072 Py_BEGIN_ALLOW_THREADS
14073 iterator->dirp = fdopendir(fd);
14074 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +010014075 } else {
14076 PyErr_SetString(PyExc_TypeError,
14077 "scandir: path should be string, bytes, os.PathLike or None, not int");
14078 return NULL;
14079 }
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014080 }
14081 else
14082#endif
14083 {
14084 if (iterator->path.narrow)
14085 path_str = iterator->path.narrow;
14086 else
14087 path_str = ".";
14088
14089 Py_BEGIN_ALLOW_THREADS
14090 iterator->dirp = opendir(path_str);
14091 Py_END_ALLOW_THREADS
14092 }
Victor Stinner6036e442015-03-08 01:58:04 +010014093
14094 if (!iterator->dirp) {
14095 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014096#ifdef HAVE_FDOPENDIR
14097 if (fd != -1) {
14098 Py_BEGIN_ALLOW_THREADS
14099 close(fd);
14100 Py_END_ALLOW_THREADS
14101 }
14102#endif
Victor Stinner6036e442015-03-08 01:58:04 +010014103 goto error;
14104 }
14105#endif
14106
14107 return (PyObject *)iterator;
14108
14109error:
14110 Py_DECREF(iterator);
14111 return NULL;
14112}
14113
Ethan Furman410ef8e2016-06-04 12:06:26 -070014114/*
14115 Return the file system path representation of the object.
14116
14117 If the object is str or bytes, then allow it to pass through with
14118 an incremented refcount. If the object defines __fspath__(), then
14119 return the result of that method. All other types raise a TypeError.
14120*/
14121PyObject *
14122PyOS_FSPath(PyObject *path)
14123{
Brett Cannon3f9183b2016-08-26 14:44:48 -070014124 /* For error message reasons, this function is manually inlined in
14125 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070014126 PyObject *func = NULL;
14127 PyObject *path_repr = NULL;
14128
14129 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
14130 Py_INCREF(path);
14131 return path;
14132 }
14133
14134 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
14135 if (NULL == func) {
14136 return PyErr_Format(PyExc_TypeError,
14137 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014138 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080014139 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070014140 }
14141
Victor Stinnerf17c3de2016-12-06 18:46:19 +010014142 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070014143 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070014144 if (NULL == path_repr) {
14145 return NULL;
14146 }
14147
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014148 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
14149 PyErr_Format(PyExc_TypeError,
14150 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080014151 "not %.200s", _PyType_Name(Py_TYPE(path)),
14152 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014153 Py_DECREF(path_repr);
14154 return NULL;
14155 }
14156
Ethan Furman410ef8e2016-06-04 12:06:26 -070014157 return path_repr;
14158}
14159
14160/*[clinic input]
14161os.fspath
14162
14163 path: object
14164
14165Return the file system path representation of the object.
14166
Brett Cannonb4f43e92016-06-09 14:32:08 -070014167If the object is str or bytes, then allow it to pass through as-is. If the
14168object defines __fspath__(), then return the result of that method. All other
14169types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070014170[clinic start generated code]*/
14171
14172static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030014173os_fspath_impl(PyObject *module, PyObject *path)
14174/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070014175{
14176 return PyOS_FSPath(path);
14177}
Victor Stinner6036e442015-03-08 01:58:04 +010014178
Victor Stinner9b1f4742016-09-06 16:18:52 -070014179#ifdef HAVE_GETRANDOM_SYSCALL
14180/*[clinic input]
14181os.getrandom
14182
14183 size: Py_ssize_t
14184 flags: int=0
14185
14186Obtain a series of random bytes.
14187[clinic start generated code]*/
14188
14189static PyObject *
14190os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
14191/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
14192{
Victor Stinner9b1f4742016-09-06 16:18:52 -070014193 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020014194 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014195
14196 if (size < 0) {
14197 errno = EINVAL;
14198 return posix_error();
14199 }
14200
Victor Stinnerec2319c2016-09-20 23:00:59 +020014201 bytes = PyBytes_FromStringAndSize(NULL, size);
14202 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070014203 PyErr_NoMemory();
14204 return NULL;
14205 }
14206
14207 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020014208 n = syscall(SYS_getrandom,
14209 PyBytes_AS_STRING(bytes),
14210 PyBytes_GET_SIZE(bytes),
14211 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070014212 if (n < 0 && errno == EINTR) {
14213 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020014214 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014215 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020014216
14217 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070014218 continue;
14219 }
14220 break;
14221 }
14222
14223 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070014224 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020014225 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014226 }
14227
Victor Stinnerec2319c2016-09-20 23:00:59 +020014228 if (n != size) {
14229 _PyBytes_Resize(&bytes, n);
14230 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070014231
14232 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020014233
14234error:
14235 Py_DECREF(bytes);
14236 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014237}
14238#endif /* HAVE_GETRANDOM_SYSCALL */
14239
Steve Dower2438cdf2019-03-29 16:37:16 -070014240#ifdef MS_WINDOWS
14241/* bpo-36085: Helper functions for managing DLL search directories
14242 * on win32
14243 */
14244
14245typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
14246typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
14247
14248/*[clinic input]
14249os._add_dll_directory
14250
14251 path: path_t
14252
14253Add a path to the DLL search path.
14254
14255This search path is used when resolving dependencies for imported
14256extension modules (the module itself is resolved through sys.path),
14257and also by ctypes.
14258
14259Returns an opaque value that may be passed to os.remove_dll_directory
14260to remove this directory from the search path.
14261[clinic start generated code]*/
14262
14263static PyObject *
14264os__add_dll_directory_impl(PyObject *module, path_t *path)
14265/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
14266{
14267 HMODULE hKernel32;
14268 PAddDllDirectory AddDllDirectory;
14269 DLL_DIRECTORY_COOKIE cookie = 0;
14270 DWORD err = 0;
14271
Saiyang Gou7514f4f2020-02-12 23:47:42 -080014272 if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
14273 return NULL;
14274 }
14275
Steve Dower2438cdf2019-03-29 16:37:16 -070014276 /* For Windows 7, we have to load this. As this will be a fairly
14277 infrequent operation, just do it each time. Kernel32 is always
14278 loaded. */
14279 Py_BEGIN_ALLOW_THREADS
14280 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
14281 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
14282 hKernel32, "AddDllDirectory")) ||
14283 !(cookie = (*AddDllDirectory)(path->wide))) {
14284 err = GetLastError();
14285 }
14286 Py_END_ALLOW_THREADS
14287
14288 if (err) {
14289 return win32_error_object_err("add_dll_directory",
14290 path->object, err);
14291 }
14292
14293 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
14294}
14295
14296/*[clinic input]
14297os._remove_dll_directory
14298
14299 cookie: object
14300
14301Removes a path from the DLL search path.
14302
14303The parameter is an opaque value that was returned from
14304os.add_dll_directory. You can only remove directories that you added
14305yourself.
14306[clinic start generated code]*/
14307
14308static PyObject *
14309os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
14310/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
14311{
14312 HMODULE hKernel32;
14313 PRemoveDllDirectory RemoveDllDirectory;
14314 DLL_DIRECTORY_COOKIE cookieValue;
14315 DWORD err = 0;
14316
14317 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
14318 PyErr_SetString(PyExc_TypeError,
14319 "Provided cookie was not returned from os.add_dll_directory");
14320 return NULL;
14321 }
14322
14323 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
14324 cookie, "DLL directory cookie");
14325
14326 /* For Windows 7, we have to load this. As this will be a fairly
14327 infrequent operation, just do it each time. Kernel32 is always
14328 loaded. */
14329 Py_BEGIN_ALLOW_THREADS
14330 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
14331 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
14332 hKernel32, "RemoveDllDirectory")) ||
14333 !(*RemoveDllDirectory)(cookieValue)) {
14334 err = GetLastError();
14335 }
14336 Py_END_ALLOW_THREADS
14337
14338 if (err) {
14339 return win32_error_object_err("remove_dll_directory",
14340 NULL, err);
14341 }
14342
14343 if (PyCapsule_SetName(cookie, NULL)) {
14344 return NULL;
14345 }
14346
14347 Py_RETURN_NONE;
14348}
14349
14350#endif
Larry Hastings31826802013-10-19 00:09:25 -070014351
Victor Stinner65a796e2020-04-01 18:49:29 +020014352
14353/* Only check if WIFEXITED is available: expect that it comes
14354 with WEXITSTATUS, WIFSIGNALED, etc.
14355
14356 os.waitstatus_to_exitcode() is implemented in C and not in Python, so
14357 subprocess can safely call it during late Python finalization without
14358 risking that used os attributes were set to None by _PyImport_Cleanup(). */
14359#if defined(WIFEXITED) || defined(MS_WINDOWS)
14360/*[clinic input]
14361os.waitstatus_to_exitcode
14362
Victor Stinner9bee32b2020-04-22 16:30:35 +020014363 status as status_obj: object
Victor Stinner65a796e2020-04-01 18:49:29 +020014364
14365Convert a wait status to an exit code.
14366
14367On Unix:
14368
14369* If WIFEXITED(status) is true, return WEXITSTATUS(status).
14370* If WIFSIGNALED(status) is true, return -WTERMSIG(status).
14371* Otherwise, raise a ValueError.
14372
14373On Windows, return status shifted right by 8 bits.
14374
14375On Unix, if the process is being traced or if waitpid() was called with
14376WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
14377This function must not be called if WIFSTOPPED(status) is true.
14378[clinic start generated code]*/
14379
14380static PyObject *
Victor Stinner9bee32b2020-04-22 16:30:35 +020014381os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
14382/*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/
Victor Stinner65a796e2020-04-01 18:49:29 +020014383{
14384#ifndef MS_WINDOWS
Victor Stinner9bee32b2020-04-22 16:30:35 +020014385 int status = _PyLong_AsInt(status_obj);
14386 if (status == -1 && PyErr_Occurred()) {
14387 return NULL;
14388 }
14389
Victor Stinner65a796e2020-04-01 18:49:29 +020014390 WAIT_TYPE wait_status;
14391 WAIT_STATUS_INT(wait_status) = status;
14392 int exitcode;
14393 if (WIFEXITED(wait_status)) {
14394 exitcode = WEXITSTATUS(wait_status);
14395 /* Sanity check to provide warranty on the function behavior.
14396 It should not occur in practice */
14397 if (exitcode < 0) {
14398 PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
14399 return NULL;
14400 }
14401 }
14402 else if (WIFSIGNALED(wait_status)) {
14403 int signum = WTERMSIG(wait_status);
14404 /* Sanity check to provide warranty on the function behavior.
14405 It should not occurs in practice */
14406 if (signum <= 0) {
14407 PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
14408 return NULL;
14409 }
14410 exitcode = -signum;
14411 } else if (WIFSTOPPED(wait_status)) {
14412 /* Status only received if the process is being traced
14413 or if waitpid() was called with WUNTRACED option. */
14414 int signum = WSTOPSIG(wait_status);
14415 PyErr_Format(PyExc_ValueError,
14416 "process stopped by delivery of signal %i",
14417 signum);
14418 return NULL;
14419 }
14420 else {
14421 PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
14422 return NULL;
14423 }
14424 return PyLong_FromLong(exitcode);
14425#else
14426 /* Windows implementation: see os.waitpid() implementation
14427 which uses _cwait(). */
Victor Stinner9bee32b2020-04-22 16:30:35 +020014428 unsigned long long status = PyLong_AsUnsignedLongLong(status_obj);
14429 if (status == (unsigned long long)-1 && PyErr_Occurred()) {
14430 return NULL;
14431 }
14432
14433 unsigned long long exitcode = (status >> 8);
14434 /* ExitProcess() accepts an UINT type:
14435 reject exit code which doesn't fit in an UINT */
14436 if (exitcode > UINT_MAX) {
14437 PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode);
14438 return NULL;
14439 }
14440 return PyLong_FromUnsignedLong((unsigned long)exitcode);
Victor Stinner65a796e2020-04-01 18:49:29 +020014441#endif
14442}
14443#endif
14444
14445
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014446static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070014447
14448 OS_STAT_METHODDEF
14449 OS_ACCESS_METHODDEF
14450 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014451 OS_CHDIR_METHODDEF
14452 OS_CHFLAGS_METHODDEF
14453 OS_CHMOD_METHODDEF
14454 OS_FCHMOD_METHODDEF
14455 OS_LCHMOD_METHODDEF
14456 OS_CHOWN_METHODDEF
14457 OS_FCHOWN_METHODDEF
14458 OS_LCHOWN_METHODDEF
14459 OS_LCHFLAGS_METHODDEF
14460 OS_CHROOT_METHODDEF
14461 OS_CTERMID_METHODDEF
14462 OS_GETCWD_METHODDEF
14463 OS_GETCWDB_METHODDEF
14464 OS_LINK_METHODDEF
14465 OS_LISTDIR_METHODDEF
14466 OS_LSTAT_METHODDEF
14467 OS_MKDIR_METHODDEF
14468 OS_NICE_METHODDEF
14469 OS_GETPRIORITY_METHODDEF
14470 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014471 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030014472 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014473 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010014474 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014475 OS_RENAME_METHODDEF
14476 OS_REPLACE_METHODDEF
14477 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014478 OS_SYMLINK_METHODDEF
14479 OS_SYSTEM_METHODDEF
14480 OS_UMASK_METHODDEF
14481 OS_UNAME_METHODDEF
14482 OS_UNLINK_METHODDEF
14483 OS_REMOVE_METHODDEF
14484 OS_UTIME_METHODDEF
14485 OS_TIMES_METHODDEF
14486 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014487 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014488 OS_EXECV_METHODDEF
14489 OS_EXECVE_METHODDEF
14490 OS_SPAWNV_METHODDEF
14491 OS_SPAWNVE_METHODDEF
14492 OS_FORK1_METHODDEF
14493 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020014494 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014495 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
14496 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
14497 OS_SCHED_GETPARAM_METHODDEF
14498 OS_SCHED_GETSCHEDULER_METHODDEF
14499 OS_SCHED_RR_GET_INTERVAL_METHODDEF
14500 OS_SCHED_SETPARAM_METHODDEF
14501 OS_SCHED_SETSCHEDULER_METHODDEF
14502 OS_SCHED_YIELD_METHODDEF
14503 OS_SCHED_SETAFFINITY_METHODDEF
14504 OS_SCHED_GETAFFINITY_METHODDEF
14505 OS_OPENPTY_METHODDEF
14506 OS_FORKPTY_METHODDEF
14507 OS_GETEGID_METHODDEF
14508 OS_GETEUID_METHODDEF
14509 OS_GETGID_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014510 OS_GETGROUPLIST_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014511 OS_GETGROUPS_METHODDEF
14512 OS_GETPID_METHODDEF
14513 OS_GETPGRP_METHODDEF
14514 OS_GETPPID_METHODDEF
14515 OS_GETUID_METHODDEF
14516 OS_GETLOGIN_METHODDEF
14517 OS_KILL_METHODDEF
14518 OS_KILLPG_METHODDEF
14519 OS_PLOCK_METHODDEF
Steve Dowercc16be82016-09-08 10:35:16 -070014520 OS_STARTFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014521 OS_SETUID_METHODDEF
14522 OS_SETEUID_METHODDEF
14523 OS_SETREUID_METHODDEF
14524 OS_SETGID_METHODDEF
14525 OS_SETEGID_METHODDEF
14526 OS_SETREGID_METHODDEF
14527 OS_SETGROUPS_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014528 OS_INITGROUPS_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014529 OS_GETPGID_METHODDEF
14530 OS_SETPGRP_METHODDEF
14531 OS_WAIT_METHODDEF
14532 OS_WAIT3_METHODDEF
14533 OS_WAIT4_METHODDEF
14534 OS_WAITID_METHODDEF
14535 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080014536 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014537 OS_GETSID_METHODDEF
14538 OS_SETSID_METHODDEF
14539 OS_SETPGID_METHODDEF
14540 OS_TCGETPGRP_METHODDEF
14541 OS_TCSETPGRP_METHODDEF
14542 OS_OPEN_METHODDEF
14543 OS_CLOSE_METHODDEF
14544 OS_CLOSERANGE_METHODDEF
14545 OS_DEVICE_ENCODING_METHODDEF
14546 OS_DUP_METHODDEF
14547 OS_DUP2_METHODDEF
14548 OS_LOCKF_METHODDEF
14549 OS_LSEEK_METHODDEF
14550 OS_READ_METHODDEF
14551 OS_READV_METHODDEF
14552 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014553 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014554 OS_WRITE_METHODDEF
14555 OS_WRITEV_METHODDEF
14556 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014557 OS_PWRITEV_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014558 OS_SENDFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014559 OS_FSTAT_METHODDEF
14560 OS_ISATTY_METHODDEF
14561 OS_PIPE_METHODDEF
14562 OS_PIPE2_METHODDEF
14563 OS_MKFIFO_METHODDEF
14564 OS_MKNOD_METHODDEF
14565 OS_MAJOR_METHODDEF
14566 OS_MINOR_METHODDEF
14567 OS_MAKEDEV_METHODDEF
14568 OS_FTRUNCATE_METHODDEF
14569 OS_TRUNCATE_METHODDEF
14570 OS_POSIX_FALLOCATE_METHODDEF
14571 OS_POSIX_FADVISE_METHODDEF
14572 OS_PUTENV_METHODDEF
14573 OS_UNSETENV_METHODDEF
14574 OS_STRERROR_METHODDEF
14575 OS_FCHDIR_METHODDEF
14576 OS_FSYNC_METHODDEF
14577 OS_SYNC_METHODDEF
14578 OS_FDATASYNC_METHODDEF
14579 OS_WCOREDUMP_METHODDEF
14580 OS_WIFCONTINUED_METHODDEF
14581 OS_WIFSTOPPED_METHODDEF
14582 OS_WIFSIGNALED_METHODDEF
14583 OS_WIFEXITED_METHODDEF
14584 OS_WEXITSTATUS_METHODDEF
14585 OS_WTERMSIG_METHODDEF
14586 OS_WSTOPSIG_METHODDEF
14587 OS_FSTATVFS_METHODDEF
14588 OS_STATVFS_METHODDEF
14589 OS_CONFSTR_METHODDEF
14590 OS_SYSCONF_METHODDEF
14591 OS_FPATHCONF_METHODDEF
14592 OS_PATHCONF_METHODDEF
14593 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030014594 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014595 OS__GETDISKUSAGE_METHODDEF
14596 OS__GETFINALPATHNAME_METHODDEF
14597 OS__GETVOLUMEPATHNAME_METHODDEF
14598 OS_GETLOADAVG_METHODDEF
14599 OS_URANDOM_METHODDEF
14600 OS_SETRESUID_METHODDEF
14601 OS_SETRESGID_METHODDEF
14602 OS_GETRESUID_METHODDEF
14603 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000014604
Larry Hastings2f936352014-08-05 14:04:04 +100014605 OS_GETXATTR_METHODDEF
14606 OS_SETXATTR_METHODDEF
14607 OS_REMOVEXATTR_METHODDEF
14608 OS_LISTXATTR_METHODDEF
14609
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014610 OS_GET_TERMINAL_SIZE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014611 OS_CPU_COUNT_METHODDEF
14612 OS_GET_INHERITABLE_METHODDEF
14613 OS_SET_INHERITABLE_METHODDEF
14614 OS_GET_HANDLE_INHERITABLE_METHODDEF
14615 OS_SET_HANDLE_INHERITABLE_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014616 OS_GET_BLOCKING_METHODDEF
14617 OS_SET_BLOCKING_METHODDEF
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014618 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070014619 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070014620 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014621 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070014622 OS__ADD_DLL_DIRECTORY_METHODDEF
14623 OS__REMOVE_DLL_DIRECTORY_METHODDEF
Victor Stinner65a796e2020-04-01 18:49:29 +020014624 OS_WAITSTATUS_TO_EXITCODE_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000014625 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014626};
14627
Barry Warsaw4a342091996-12-19 23:50:02 +000014628static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014629all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000014630{
Guido van Rossum94f6f721999-01-06 18:42:14 +000014631#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014632 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014633#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014634#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014635 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014636#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014637#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014638 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014639#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014640#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014641 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014642#endif
Fred Drakec9680921999-12-13 16:37:25 +000014643#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014644 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000014645#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014646#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014647 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014648#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014649#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014650 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014651#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014652#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014653 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014654#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014655#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014656 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014657#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014658#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014659 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014660#endif
14661#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014662 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014663#endif
14664#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014665 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014666#endif
14667#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014668 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014669#endif
14670#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014671 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014672#endif
14673#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014674 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014675#endif
14676#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014677 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014678#endif
14679#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014680 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014681#endif
14682#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014683 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014684#endif
14685#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014686 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014687#endif
14688#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014689 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014690#endif
14691#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014692 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014693#endif
14694#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014695 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014696#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000014697#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014698 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014699#endif
14700#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014701 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014702#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014703#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014704 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014705#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014706#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014707 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014708#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014709#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000014710#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014711 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014712#endif
14713#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014714 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014715#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014716#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014717#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014718 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014719#endif
14720#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014721 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014722#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014723#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014724 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014725#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014726#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014727 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014728#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020014729#ifdef O_TMPFILE
14730 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
14731#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014732#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014733 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014734#endif
14735#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014736 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014737#endif
14738#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014739 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014740#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020014741#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014742 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020014743#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014744#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014745 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014746#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014747
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014748
Jesus Cea94363612012-06-22 18:32:07 +020014749#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014750 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014751#endif
14752#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014753 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014754#endif
14755
Tim Peters5aa91602002-01-30 05:46:57 +000014756/* MS Windows */
14757#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000014758 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014759 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014760#endif
14761#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000014762 /* Optimize for short life (keep in memory). */
14763 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014764 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014765#endif
14766#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000014767 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014768 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014769#endif
14770#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000014771 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014772 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014773#endif
14774#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000014775 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014776 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014777#endif
14778
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014779/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014780#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000014781 /* Send a SIGIO signal whenever input or output
14782 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014783 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014784#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014785#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000014786 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014787 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014788#endif
14789#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000014790 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014791 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014792#endif
14793#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000014794 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014795 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014796#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014797#ifdef O_NOLINKS
14798 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014799 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014800#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014801#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000014802 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014803 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014804#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000014805
Victor Stinner8c62be82010-05-06 00:08:46 +000014806 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014807#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014808 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014809#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014810#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014811 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014812#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014813#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014814 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014815#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014816#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014817 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014818#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014819#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014820 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014821#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014822#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014823 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014824#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014825#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014826 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014827#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014828#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014829 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014830#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014831#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014832 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014833#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014834#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014835 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014836#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014837#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014838 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014839#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014840#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014841 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014842#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014843#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014844 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014845#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014846#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014847 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014848#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014849#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014850 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014851#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014852#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014853 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014854#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014855#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014856 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014857#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014858
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000014859 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014860#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014861 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014862#endif /* ST_RDONLY */
14863#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014864 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014865#endif /* ST_NOSUID */
14866
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014867 /* GNU extensions */
14868#ifdef ST_NODEV
14869 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14870#endif /* ST_NODEV */
14871#ifdef ST_NOEXEC
14872 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14873#endif /* ST_NOEXEC */
14874#ifdef ST_SYNCHRONOUS
14875 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14876#endif /* ST_SYNCHRONOUS */
14877#ifdef ST_MANDLOCK
14878 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14879#endif /* ST_MANDLOCK */
14880#ifdef ST_WRITE
14881 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14882#endif /* ST_WRITE */
14883#ifdef ST_APPEND
14884 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14885#endif /* ST_APPEND */
14886#ifdef ST_NOATIME
14887 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14888#endif /* ST_NOATIME */
14889#ifdef ST_NODIRATIME
14890 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14891#endif /* ST_NODIRATIME */
14892#ifdef ST_RELATIME
14893 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14894#endif /* ST_RELATIME */
14895
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014896 /* FreeBSD sendfile() constants */
14897#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014898 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014899#endif
14900#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014901 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014902#endif
14903#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014904 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014905#endif
14906
Ross Lagerwall7807c352011-03-17 20:20:30 +020014907 /* constants for posix_fadvise */
14908#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014909 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014910#endif
14911#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014912 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014913#endif
14914#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014915 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014916#endif
14917#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014918 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014919#endif
14920#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014921 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014922#endif
14923#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014924 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014925#endif
14926
14927 /* constants for waitid */
14928#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014929 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14930 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14931 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080014932#ifdef P_PIDFD
14933 if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
14934#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014935#endif
14936#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014937 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014938#endif
14939#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014940 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014941#endif
14942#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014943 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014944#endif
14945#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014946 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014947#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014948#ifdef CLD_KILLED
14949 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
14950#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014951#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014952 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014953#endif
14954#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014955 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014956#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014957#ifdef CLD_STOPPED
14958 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
14959#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014960#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014961 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014962#endif
14963
14964 /* constants for lockf */
14965#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014966 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014967#endif
14968#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014969 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014970#endif
14971#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014972 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014973#endif
14974#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014975 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014976#endif
14977
Pablo Galindo4defba32018-01-27 16:16:37 +000014978#ifdef RWF_DSYNC
14979 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14980#endif
14981#ifdef RWF_HIPRI
14982 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14983#endif
14984#ifdef RWF_SYNC
14985 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14986#endif
14987#ifdef RWF_NOWAIT
14988 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14989#endif
YoSTEALTH76ef2552020-05-27 15:32:22 -060014990#ifdef RWF_APPEND
14991 if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1;
14992#endif
Pablo Galindo4defba32018-01-27 16:16:37 +000014993
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014994/* constants for posix_spawn */
14995#ifdef HAVE_POSIX_SPAWN
14996 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14997 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14998 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14999#endif
15000
pxinwrf2d7ac72019-05-21 18:46:37 +080015001#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015002 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
15003 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015004 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080015005#endif
15006#ifdef HAVE_SPAWNV
15007 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015008 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000015009#endif
15010
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015011#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015012#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015013 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015014#endif
15015#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015016 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015017#endif
15018#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015019 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015020#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015021#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080015022 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015023#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015024#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015025 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015026#endif
15027#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015028 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015029#endif
15030#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015031 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015032#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015033#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015034 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015035#endif
15036#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015037 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015038#endif
15039#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015040 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015041#endif
15042#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015043 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015044#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015045#endif
15046
Benjamin Peterson9428d532011-09-14 11:45:52 -040015047#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015048 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
15049 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
15050 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040015051#endif
15052
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015053#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015054 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015055#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015056#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015057 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015058#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015059#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015060 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015061#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015062#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015063 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015064#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015065#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015066 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015067#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015068#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015069 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015070#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015071#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015072 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015073#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010015074#if HAVE_DECL_RTLD_MEMBER
15075 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
15076#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020015077
Victor Stinner9b1f4742016-09-06 16:18:52 -070015078#ifdef HAVE_GETRANDOM_SYSCALL
15079 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
15080 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
15081#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015082#ifdef HAVE_MEMFD_CREATE
15083 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
15084 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
15085#ifdef MFD_HUGETLB
15086 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015087#endif
15088#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015089 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015090#endif
15091#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015092 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015093#endif
15094#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015095 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015096#endif
15097#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015098 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015099#endif
15100#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015101 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015102#endif
15103#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015104 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015105#endif
15106#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015107 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015108#endif
15109#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015110 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015111#endif
15112#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015113 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015114#endif
15115#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015116 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015117#endif
15118#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015119 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015120#endif
15121#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015122 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015123#endif
15124#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015125 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015126#endif
15127#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015128 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
15129#endif
15130#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070015131
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020015132#if defined(__APPLE__)
15133 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
15134#endif
15135
Steve Dower2438cdf2019-03-29 16:37:16 -070015136#ifdef MS_WINDOWS
15137 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
15138 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
15139 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
15140 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
15141 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
15142#endif
15143
Victor Stinner8c62be82010-05-06 00:08:46 +000015144 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000015145}
15146
15147
Ronald Oussoren41761932020-11-08 10:05:27 +010015148
15149#define PROBE(name, test) \
15150 static int name(void) \
15151 { \
15152 if (test) { \
15153 return 1; \
15154 } else { \
15155 return 0; \
15156 } \
15157 }
15158
15159#ifdef HAVE_FSTATAT
15160PROBE(probe_fstatat, HAVE_FSTATAT_RUNTIME)
15161#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070015162
15163#ifdef HAVE_FACCESSAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015164PROBE(probe_faccessat, HAVE_FACCESSAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015165#endif
15166
15167#ifdef HAVE_FCHMODAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015168PROBE(probe_fchmodat, HAVE_FCHMODAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015169#endif
15170
Larry Hastings00964ed2013-08-12 13:49:30 -040015171#ifdef HAVE_FCHOWNAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015172PROBE(probe_fchownat, HAVE_FCHOWNAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015173#endif
15174
15175#ifdef HAVE_LINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015176PROBE(probe_linkat, HAVE_LINKAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015177#endif
15178
Ronald Oussoren41761932020-11-08 10:05:27 +010015179#ifdef HAVE_FDOPENDIR
15180PROBE(probe_fdopendir, HAVE_FDOPENDIR_RUNTIME)
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015181#endif
15182
Larry Hastings9cf065c2012-06-22 16:30:09 -070015183#ifdef HAVE_MKDIRAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015184PROBE(probe_mkdirat, HAVE_MKDIRAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015185#endif
15186
15187#ifdef HAVE_RENAMEAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015188PROBE(probe_renameat, HAVE_RENAMEAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015189#endif
15190
15191#ifdef HAVE_UNLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015192PROBE(probe_unlinkat, HAVE_UNLINKAT_RUNTIME)
15193#endif
15194
15195#ifdef HAVE_OPENAT
15196PROBE(probe_openat, HAVE_OPENAT_RUNTIME)
15197#endif
15198
15199#ifdef HAVE_READLINKAT
15200PROBE(probe_readlinkat, HAVE_READLINKAT_RUNTIME)
15201#endif
15202
15203#ifdef HAVE_SYMLINKAT
15204PROBE(probe_symlinkat, HAVE_SYMLINKAT_RUNTIME)
15205#endif
15206
15207#ifdef HAVE_FUTIMENS
15208PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015209#endif
15210
15211#ifdef HAVE_UTIMENSAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015212PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME)
15213#endif
15214
15215
15216
15217
15218static const struct have_function {
15219 const char * const label;
15220 int (*probe)(void);
15221} have_functions[] = {
15222
15223#ifdef HAVE_FACCESSAT
15224 { "HAVE_FACCESSAT", probe_faccessat },
15225#endif
15226
15227#ifdef HAVE_FCHDIR
15228 { "HAVE_FCHDIR", NULL },
15229#endif
15230
15231#ifdef HAVE_FCHMOD
15232 { "HAVE_FCHMOD", NULL },
15233#endif
15234
15235#ifdef HAVE_FCHMODAT
15236 { "HAVE_FCHMODAT", probe_fchmodat },
15237#endif
15238
15239#ifdef HAVE_FCHOWN
15240 { "HAVE_FCHOWN", NULL },
15241#endif
15242
15243#ifdef HAVE_FCHOWNAT
15244 { "HAVE_FCHOWNAT", probe_fchownat },
15245#endif
15246
15247#ifdef HAVE_FEXECVE
15248 { "HAVE_FEXECVE", NULL },
15249#endif
15250
15251#ifdef HAVE_FDOPENDIR
15252 { "HAVE_FDOPENDIR", probe_fdopendir },
15253#endif
15254
15255#ifdef HAVE_FPATHCONF
15256 { "HAVE_FPATHCONF", NULL },
15257#endif
15258
15259#ifdef HAVE_FSTATAT
15260 { "HAVE_FSTATAT", probe_fstatat },
15261#endif
15262
15263#ifdef HAVE_FSTATVFS
15264 { "HAVE_FSTATVFS", NULL },
15265#endif
15266
15267#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
15268 { "HAVE_FTRUNCATE", NULL },
15269#endif
15270
15271#ifdef HAVE_FUTIMENS
15272 { "HAVE_FUTIMENS", probe_futimens },
15273#endif
15274
15275#ifdef HAVE_FUTIMES
15276 { "HAVE_FUTIMES", NULL },
15277#endif
15278
15279#ifdef HAVE_FUTIMESAT
15280 { "HAVE_FUTIMESAT", NULL },
15281#endif
15282
15283#ifdef HAVE_LINKAT
15284 { "HAVE_LINKAT", probe_linkat },
15285#endif
15286
15287#ifdef HAVE_LCHFLAGS
15288 { "HAVE_LCHFLAGS", NULL },
15289#endif
15290
15291#ifdef HAVE_LCHMOD
15292 { "HAVE_LCHMOD", NULL },
15293#endif
15294
15295#ifdef HAVE_LCHOWN
15296 { "HAVE_LCHOWN", NULL },
15297#endif
15298
15299#ifdef HAVE_LSTAT
15300 { "HAVE_LSTAT", NULL },
15301#endif
15302
15303#ifdef HAVE_LUTIMES
15304 { "HAVE_LUTIMES", NULL },
15305#endif
15306
15307#ifdef HAVE_MEMFD_CREATE
15308 { "HAVE_MEMFD_CREATE", NULL },
15309#endif
15310
15311#ifdef HAVE_MKDIRAT
15312 { "HAVE_MKDIRAT", probe_mkdirat },
15313#endif
15314
15315#ifdef HAVE_MKFIFOAT
15316 { "HAVE_MKFIFOAT", NULL },
15317#endif
15318
15319#ifdef HAVE_MKNODAT
15320 { "HAVE_MKNODAT", NULL },
15321#endif
15322
15323#ifdef HAVE_OPENAT
15324 { "HAVE_OPENAT", probe_openat },
15325#endif
15326
15327#ifdef HAVE_READLINKAT
15328 { "HAVE_READLINKAT", probe_readlinkat },
15329#endif
15330
15331#ifdef HAVE_RENAMEAT
15332 { "HAVE_RENAMEAT", probe_renameat },
15333#endif
15334
15335#ifdef HAVE_SYMLINKAT
15336 { "HAVE_SYMLINKAT", probe_symlinkat },
15337#endif
15338
15339#ifdef HAVE_UNLINKAT
15340 { "HAVE_UNLINKAT", probe_unlinkat },
15341#endif
15342
15343#ifdef HAVE_UTIMENSAT
15344 { "HAVE_UTIMENSAT", probe_utimensat },
Larry Hastings9cf065c2012-06-22 16:30:09 -070015345#endif
15346
15347#ifdef MS_WINDOWS
Ronald Oussoren41761932020-11-08 10:05:27 +010015348 { "MS_WINDOWS", NULL },
Larry Hastings9cf065c2012-06-22 16:30:09 -070015349#endif
15350
Ronald Oussoren41761932020-11-08 10:05:27 +010015351 { NULL, NULL }
Larry Hastings9cf065c2012-06-22 16:30:09 -070015352};
15353
15354
Victor Stinner1c2fa782020-05-10 11:05:29 +020015355static int
15356posixmodule_exec(PyObject *m)
Guido van Rossumb6775db1994-08-01 11:34:53 +000015357{
Victor Stinner97f33c32020-05-14 18:05:58 +020015358 _posixstate *state = get_posix_state(m);
Tim Peters5aa91602002-01-30 05:46:57 +000015359
Ronald Oussoren41761932020-11-08 10:05:27 +010015360#if defined(HAVE_PWRITEV)
15361 if (HAVE_PWRITEV_RUNTIME) {} else {
15362 PyObject* dct = PyModule_GetDict(m);
15363
15364 if (dct == NULL) {
15365 return -1;
15366 }
15367
15368 if (PyDict_DelItemString(dct, "pwritev") == -1) {
15369 PyErr_Clear();
15370 }
15371 if (PyDict_DelItemString(dct, "preadv") == -1) {
15372 PyErr_Clear();
15373 }
15374 }
15375#endif
15376
Victor Stinner8c62be82010-05-06 00:08:46 +000015377 /* Initialize environ dictionary */
Victor Stinner97f33c32020-05-14 18:05:58 +020015378 PyObject *v = convertenviron();
Victor Stinner8c62be82010-05-06 00:08:46 +000015379 Py_XINCREF(v);
15380 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015381 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +000015382 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000015383
Victor Stinner8c62be82010-05-06 00:08:46 +000015384 if (all_ins(m))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015385 return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000015386
Victor Stinner8c62be82010-05-06 00:08:46 +000015387 if (setup_confname_tables(m))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015388 return -1;
Fred Drakebec628d1999-12-15 18:31:10 +000015389
Victor Stinner8c62be82010-05-06 00:08:46 +000015390 Py_INCREF(PyExc_OSError);
15391 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000015392
Ross Lagerwall7807c352011-03-17 20:20:30 +020015393#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080015394 waitid_result_desc.name = MODNAME ".waitid_result";
15395 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
15396 if (WaitidResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015397 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015398 }
15399 Py_INCREF(WaitidResultType);
15400 PyModule_AddObject(m, "waitid_result", WaitidResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015401 state->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015402#endif
15403
Eddie Elizondob3966632019-11-05 07:16:14 -080015404 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
15405 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
15406 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
15407 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
15408 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
15409 if (StatResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015410 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015411 }
15412 Py_INCREF(StatResultType);
15413 PyModule_AddObject(m, "stat_result", StatResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015414 state->StatResultType = StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015415 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
15416 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015417
Eddie Elizondob3966632019-11-05 07:16:14 -080015418 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
15419 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
15420 if (StatVFSResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015421 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015422 }
15423 Py_INCREF(StatVFSResultType);
15424 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015425 state->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015426#ifdef NEED_TICKS_PER_SECOND
15427# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080015428 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015429# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080015430 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015431# else
Eddie Elizondob3966632019-11-05 07:16:14 -080015432 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015433# endif
15434#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015435
William Orr81574b82018-10-01 22:19:56 -070015436#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080015437 sched_param_desc.name = MODNAME ".sched_param";
15438 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
15439 if (SchedParamType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015440 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +000015441 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015442 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080015443 PyModule_AddObject(m, "sched_param", SchedParamType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015444 state->SchedParamType = SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015445 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050015446#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000015447
Eddie Elizondob3966632019-11-05 07:16:14 -080015448 /* initialize TerminalSize_info */
15449 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
15450 if (TerminalSizeType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015451 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015452 }
15453 Py_INCREF(TerminalSizeType);
15454 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015455 state->TerminalSizeType = TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015456
15457 /* initialize scandir types */
Victor Stinner1c2fa782020-05-10 11:05:29 +020015458 PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL);
Eddie Elizondob3966632019-11-05 07:16:14 -080015459 if (ScandirIteratorType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015460 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015461 }
Victor Stinner97f33c32020-05-14 18:05:58 +020015462 state->ScandirIteratorType = ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015463
Victor Stinner1c2fa782020-05-10 11:05:29 +020015464 PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
Eddie Elizondob3966632019-11-05 07:16:14 -080015465 if (DirEntryType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015466 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015467 }
15468 Py_INCREF(DirEntryType);
15469 PyModule_AddObject(m, "DirEntry", DirEntryType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015470 state->DirEntryType = DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015471
Larry Hastings605a62d2012-06-24 04:33:36 -070015472 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080015473 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015474 if (TimesResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015475 return -1;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015476 }
Eddie Elizondob3966632019-11-05 07:16:14 -080015477 Py_INCREF(TimesResultType);
15478 PyModule_AddObject(m, "times_result", TimesResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015479 state->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070015480
Eddie Elizondob3966632019-11-05 07:16:14 -080015481 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015482 if (UnameResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015483 return -1;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015484 }
Eddie Elizondob3966632019-11-05 07:16:14 -080015485 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015486 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015487 state->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070015488
Victor Stinner97f33c32020-05-14 18:05:58 +020015489 if ((state->billion = PyLong_FromLong(1000000000)) == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015490 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015491#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Victor Stinner97f33c32020-05-14 18:05:58 +020015492 state->struct_rusage = PyUnicode_InternFromString("struct_rusage");
15493 if (state->struct_rusage == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015494 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015495#endif
Victor Stinner97f33c32020-05-14 18:05:58 +020015496 state->st_mode = PyUnicode_InternFromString("st_mode");
15497 if (state->st_mode == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015498 return -1;
Larry Hastings6fe20b32012-04-19 15:07:49 -070015499
Larry Hastings9cf065c2012-06-22 16:30:09 -070015500 /* suppress "function not used" warnings */
15501 {
15502 int ignored;
15503 fd_specified("", -1);
15504 follow_symlinks_specified("", 1);
15505 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
15506 dir_fd_converter(Py_None, &ignored);
15507 dir_fd_unavailable(Py_None, &ignored);
15508 }
15509
15510 /*
15511 * provide list of locally available functions
15512 * so os.py can populate support_* lists
15513 */
Victor Stinner97f33c32020-05-14 18:05:58 +020015514 PyObject *list = PyList_New(0);
15515 if (!list) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015516 return -1;
Victor Stinner97f33c32020-05-14 18:05:58 +020015517 }
Ronald Oussoren41761932020-11-08 10:05:27 +010015518 for (const struct have_function *trace = have_functions; trace->label; trace++) {
15519 PyObject *unicode;
15520 if (trace->probe && !trace->probe()) continue;
15521 unicode = PyUnicode_DecodeASCII(trace->label, strlen(trace->label), NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -070015522 if (!unicode)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015523 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -070015524 if (PyList_Append(list, unicode))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015525 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -070015526 Py_DECREF(unicode);
15527 }
Ronald Oussoren41761932020-11-08 10:05:27 +010015528
Larry Hastings9cf065c2012-06-22 16:30:09 -070015529 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040015530
Victor Stinner1c2fa782020-05-10 11:05:29 +020015531 return 0;
15532}
15533
15534
15535static PyModuleDef_Slot posixmodile_slots[] = {
15536 {Py_mod_exec, posixmodule_exec},
15537 {0, NULL}
15538};
15539
15540static struct PyModuleDef posixmodule = {
15541 PyModuleDef_HEAD_INIT,
15542 .m_name = MODNAME,
15543 .m_doc = posix__doc__,
15544 .m_size = sizeof(_posixstate),
15545 .m_methods = posix_methods,
15546 .m_slots = posixmodile_slots,
15547 .m_traverse = _posix_traverse,
15548 .m_clear = _posix_clear,
15549 .m_free = _posix_free,
15550};
15551
15552PyMODINIT_FUNC
15553INITFUNC(void)
15554{
15555 return PyModuleDef_Init(&posixmodule);
Guido van Rossumb6775db1994-08-01 11:34:53 +000015556}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015557
15558#ifdef __cplusplus
15559}
15560#endif