blob: 70b47c475feaa568197333efb22c370a37b30309 [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 }
9876#endif
9877
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009878 do {
9879 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009880 ret = sendfile(out_fd, in_fd, &offset, count);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009881 Py_END_ALLOW_THREADS
9882 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009883 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009884 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009885 return Py_BuildValue("n", ret);
9886#endif
9887}
Larry Hastings2f936352014-08-05 14:04:04 +10009888#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009889
Larry Hastings2f936352014-08-05 14:04:04 +10009890
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009891#if defined(__APPLE__)
9892/*[clinic input]
9893os._fcopyfile
9894
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009895 in_fd: int
9896 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009897 flags: int
9898 /
9899
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009900Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009901[clinic start generated code]*/
9902
9903static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009904os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9905/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009906{
9907 int ret;
9908
9909 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009910 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009911 Py_END_ALLOW_THREADS
9912 if (ret < 0)
9913 return posix_error();
9914 Py_RETURN_NONE;
9915}
9916#endif
9917
9918
Larry Hastings2f936352014-08-05 14:04:04 +10009919/*[clinic input]
9920os.fstat
9921
9922 fd : int
9923
9924Perform a stat system call on the given file descriptor.
9925
9926Like stat(), but for an open file descriptor.
9927Equivalent to os.stat(fd).
9928[clinic start generated code]*/
9929
Larry Hastings2f936352014-08-05 14:04:04 +10009930static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009931os_fstat_impl(PyObject *module, int fd)
9932/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009933{
Victor Stinner8c62be82010-05-06 00:08:46 +00009934 STRUCT_STAT st;
9935 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009936 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009937
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009938 do {
9939 Py_BEGIN_ALLOW_THREADS
9940 res = FSTAT(fd, &st);
9941 Py_END_ALLOW_THREADS
9942 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009943 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009944#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009945 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009946#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009947 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009948#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009949 }
Tim Peters5aa91602002-01-30 05:46:57 +00009950
Victor Stinner1c2fa782020-05-10 11:05:29 +02009951 return _pystat_fromstructstat(module, &st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009952}
9953
Larry Hastings2f936352014-08-05 14:04:04 +10009954
9955/*[clinic input]
9956os.isatty -> bool
9957 fd: int
9958 /
9959
9960Return True if the fd is connected to a terminal.
9961
9962Return True if the file descriptor is an open file descriptor
9963connected to the slave end of a terminal.
9964[clinic start generated code]*/
9965
Larry Hastings2f936352014-08-05 14:04:04 +10009966static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009967os_isatty_impl(PyObject *module, int fd)
9968/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009969{
Steve Dower8fc89802015-04-12 00:26:27 -04009970 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009971 _Py_BEGIN_SUPPRESS_IPH
9972 return_value = isatty(fd);
9973 _Py_END_SUPPRESS_IPH
9974 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009975}
9976
9977
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009978#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009979/*[clinic input]
9980os.pipe
9981
9982Create a pipe.
9983
9984Returns a tuple of two file descriptors:
9985 (read_fd, write_fd)
9986[clinic start generated code]*/
9987
Larry Hastings2f936352014-08-05 14:04:04 +10009988static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009989os_pipe_impl(PyObject *module)
9990/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009991{
Victor Stinner8c62be82010-05-06 00:08:46 +00009992 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009993#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009994 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009995 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009996 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009997#else
9998 int res;
9999#endif
10000
10001#ifdef MS_WINDOWS
10002 attr.nLength = sizeof(attr);
10003 attr.lpSecurityDescriptor = NULL;
10004 attr.bInheritHandle = FALSE;
10005
10006 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -080010007 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +020010008 ok = CreatePipe(&read, &write, &attr, 0);
10009 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -070010010 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
10011 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +020010012 if (fds[0] == -1 || fds[1] == -1) {
10013 CloseHandle(read);
10014 CloseHandle(write);
10015 ok = 0;
10016 }
10017 }
Steve Dowerc3630612016-11-19 18:41:16 -080010018 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +020010019 Py_END_ALLOW_THREADS
10020
Victor Stinner8c62be82010-05-06 00:08:46 +000010021 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +010010022 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +020010023#else
10024
10025#ifdef HAVE_PIPE2
10026 Py_BEGIN_ALLOW_THREADS
10027 res = pipe2(fds, O_CLOEXEC);
10028 Py_END_ALLOW_THREADS
10029
10030 if (res != 0 && errno == ENOSYS)
10031 {
10032#endif
10033 Py_BEGIN_ALLOW_THREADS
10034 res = pipe(fds);
10035 Py_END_ALLOW_THREADS
10036
10037 if (res == 0) {
10038 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
10039 close(fds[0]);
10040 close(fds[1]);
10041 return NULL;
10042 }
10043 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
10044 close(fds[0]);
10045 close(fds[1]);
10046 return NULL;
10047 }
10048 }
10049#ifdef HAVE_PIPE2
10050 }
10051#endif
10052
10053 if (res != 0)
10054 return PyErr_SetFromErrno(PyExc_OSError);
10055#endif /* !MS_WINDOWS */
10056 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +000010057}
Guido van Rossuma4916fa1996-05-23 22:58:55 +000010058#endif /* HAVE_PIPE */
10059
Larry Hastings2f936352014-08-05 14:04:04 +100010060
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010061#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +100010062/*[clinic input]
10063os.pipe2
10064
10065 flags: int
10066 /
10067
10068Create a pipe with flags set atomically.
10069
10070Returns a tuple of two file descriptors:
10071 (read_fd, write_fd)
10072
10073flags can be constructed by ORing together one or more of these values:
10074O_NONBLOCK, O_CLOEXEC.
10075[clinic start generated code]*/
10076
Larry Hastings2f936352014-08-05 14:04:04 +100010077static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010078os_pipe2_impl(PyObject *module, int flags)
10079/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010080{
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010081 int fds[2];
10082 int res;
10083
Charles-François Natalidaafdd52011-05-29 20:07:40 +020010084 res = pipe2(fds, flags);
10085 if (res != 0)
10086 return posix_error();
10087 return Py_BuildValue("(ii)", fds[0], fds[1]);
10088}
10089#endif /* HAVE_PIPE2 */
10090
Larry Hastings2f936352014-08-05 14:04:04 +100010091
Ross Lagerwall7807c352011-03-17 20:20:30 +020010092#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +100010093/*[clinic input]
10094os.writev -> Py_ssize_t
10095 fd: int
10096 buffers: object
10097 /
10098
10099Iterate over buffers, and write the contents of each to a file descriptor.
10100
10101Returns the total number of bytes written.
10102buffers must be a sequence of bytes-like objects.
10103[clinic start generated code]*/
10104
Larry Hastings2f936352014-08-05 14:04:04 +100010105static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010106os_writev_impl(PyObject *module, int fd, PyObject *buffers)
10107/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010108{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +030010109 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +100010110 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010111 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010112 struct iovec *iov;
10113 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +100010114
10115 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +020010116 PyErr_SetString(PyExc_TypeError,
10117 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +100010118 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010119 }
Larry Hastings2f936352014-08-05 14:04:04 +100010120 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +030010121 if (cnt < 0)
10122 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010123
Larry Hastings2f936352014-08-05 14:04:04 +100010124 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
10125 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010126 }
10127
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010128 do {
10129 Py_BEGIN_ALLOW_THREADS
10130 result = writev(fd, iov, cnt);
10131 Py_END_ALLOW_THREADS
10132 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +020010133
10134 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010135 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +100010136 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +010010137
Georg Brandl306336b2012-06-24 12:55:33 +020010138 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010139}
Larry Hastings2f936352014-08-05 14:04:04 +100010140#endif /* HAVE_WRITEV */
10141
10142
10143#ifdef HAVE_PWRITE
10144/*[clinic input]
10145os.pwrite -> Py_ssize_t
10146
10147 fd: int
10148 buffer: Py_buffer
10149 offset: Py_off_t
10150 /
10151
10152Write bytes to a file descriptor starting at a particular offset.
10153
10154Write buffer to fd, starting at offset bytes from the beginning of
10155the file. Returns the number of bytes writte. Does not change the
10156current file offset.
10157[clinic start generated code]*/
10158
Larry Hastings2f936352014-08-05 14:04:04 +100010159static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010160os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
10161/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010162{
10163 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010164 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010165
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010166 do {
10167 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -040010168 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010169 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -040010170 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010171 Py_END_ALLOW_THREADS
10172 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010173
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010174 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +100010175 posix_error();
10176 return size;
10177}
10178#endif /* HAVE_PWRITE */
10179
Pablo Galindo4defba32018-01-27 16:16:37 +000010180#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
10181/*[clinic input]
10182os.pwritev -> Py_ssize_t
10183
10184 fd: int
10185 buffers: object
10186 offset: Py_off_t
10187 flags: int = 0
10188 /
10189
10190Writes the contents of bytes-like objects to a file descriptor at a given offset.
10191
10192Combines the functionality of writev() and pwrite(). All buffers must be a sequence
10193of bytes-like objects. Buffers are processed in array order. Entire contents of first
10194buffer is written before proceeding to second, and so on. The operating system may
10195set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
10196This function writes the contents of each object to the file descriptor and returns
10197the total number of bytes written.
10198
10199The flags argument contains a bitwise OR of zero or more of the following flags:
10200
10201- RWF_DSYNC
10202- RWF_SYNC
YoSTEALTH76ef2552020-05-27 15:32:22 -060010203- RWF_APPEND
Pablo Galindo4defba32018-01-27 16:16:37 +000010204
10205Using non-zero flags requires Linux 4.7 or newer.
10206[clinic start generated code]*/
10207
10208static Py_ssize_t
10209os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
10210 int flags)
YoSTEALTH76ef2552020-05-27 15:32:22 -060010211/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=35358c327e1a2a8e]*/
Pablo Galindo4defba32018-01-27 16:16:37 +000010212{
10213 Py_ssize_t cnt;
10214 Py_ssize_t result;
10215 int async_err = 0;
10216 struct iovec *iov;
10217 Py_buffer *buf;
10218
10219 if (!PySequence_Check(buffers)) {
10220 PyErr_SetString(PyExc_TypeError,
10221 "pwritev() arg 2 must be a sequence");
10222 return -1;
10223 }
10224
10225 cnt = PySequence_Size(buffers);
10226 if (cnt < 0) {
10227 return -1;
10228 }
10229
10230#ifndef HAVE_PWRITEV2
10231 if(flags != 0) {
10232 argument_unavailable_error("pwritev2", "flags");
10233 return -1;
10234 }
10235#endif
10236
10237 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
10238 return -1;
10239 }
10240#ifdef HAVE_PWRITEV2
10241 do {
10242 Py_BEGIN_ALLOW_THREADS
10243 _Py_BEGIN_SUPPRESS_IPH
10244 result = pwritev2(fd, iov, cnt, offset, flags);
10245 _Py_END_SUPPRESS_IPH
10246 Py_END_ALLOW_THREADS
10247 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10248#else
Ronald Oussoren41761932020-11-08 10:05:27 +010010249
10250#ifdef __APPLE__
10251/* This entire function will be removed from the module dict when the API
10252 * is not available.
10253 */
10254#pragma clang diagnostic push
10255#pragma clang diagnostic ignored "-Wunguarded-availability"
10256#pragma clang diagnostic ignored "-Wunguarded-availability-new"
10257#endif
Pablo Galindo4defba32018-01-27 16:16:37 +000010258 do {
10259 Py_BEGIN_ALLOW_THREADS
10260 _Py_BEGIN_SUPPRESS_IPH
10261 result = pwritev(fd, iov, cnt, offset);
10262 _Py_END_SUPPRESS_IPH
10263 Py_END_ALLOW_THREADS
10264 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ronald Oussoren41761932020-11-08 10:05:27 +010010265
10266#ifdef __APPLE__
10267#pragma clang diagnostic pop
10268#endif
10269
Pablo Galindo4defba32018-01-27 16:16:37 +000010270#endif
10271
10272 iov_cleanup(iov, buf, cnt);
10273 if (result < 0) {
10274 if (!async_err) {
10275 posix_error();
10276 }
10277 return -1;
10278 }
10279
10280 return result;
10281}
10282#endif /* HAVE_PWRITEV */
10283
Pablo Galindoaac4d032019-05-31 19:39:47 +010010284#ifdef HAVE_COPY_FILE_RANGE
10285/*[clinic input]
10286
10287os.copy_file_range
10288 src: int
10289 Source file descriptor.
10290 dst: int
10291 Destination file descriptor.
10292 count: Py_ssize_t
10293 Number of bytes to copy.
10294 offset_src: object = None
10295 Starting offset in src.
10296 offset_dst: object = None
10297 Starting offset in dst.
10298
10299Copy count bytes from one file descriptor to another.
10300
10301If offset_src is None, then src is read from the current position;
10302respectively for offset_dst.
10303[clinic start generated code]*/
10304
10305static PyObject *
10306os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
10307 PyObject *offset_src, PyObject *offset_dst)
10308/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
10309{
10310 off_t offset_src_val, offset_dst_val;
10311 off_t *p_offset_src = NULL;
10312 off_t *p_offset_dst = NULL;
10313 Py_ssize_t ret;
10314 int async_err = 0;
10315 /* The flags argument is provided to allow
10316 * for future extensions and currently must be to 0. */
10317 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +000010318
10319
Pablo Galindoaac4d032019-05-31 19:39:47 +010010320 if (count < 0) {
10321 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
10322 return NULL;
10323 }
10324
10325 if (offset_src != Py_None) {
10326 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
10327 return NULL;
10328 }
10329 p_offset_src = &offset_src_val;
10330 }
10331
10332 if (offset_dst != Py_None) {
10333 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
10334 return NULL;
10335 }
10336 p_offset_dst = &offset_dst_val;
10337 }
10338
10339 do {
10340 Py_BEGIN_ALLOW_THREADS
10341 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
10342 Py_END_ALLOW_THREADS
10343 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
10344
10345 if (ret < 0) {
10346 return (!async_err) ? posix_error() : NULL;
10347 }
10348
10349 return PyLong_FromSsize_t(ret);
10350}
10351#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +100010352
10353#ifdef HAVE_MKFIFO
10354/*[clinic input]
10355os.mkfifo
10356
10357 path: path_t
10358 mode: int=0o666
10359 *
10360 dir_fd: dir_fd(requires='mkfifoat')=None
10361
10362Create a "fifo" (a POSIX named pipe).
10363
10364If dir_fd is not None, it should be a file descriptor open to a directory,
10365 and path should be relative; path will then be relative to that directory.
10366dir_fd may not be implemented on your platform.
10367 If it is unavailable, using it will raise a NotImplementedError.
10368[clinic start generated code]*/
10369
Larry Hastings2f936352014-08-05 14:04:04 +100010370static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010371os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
10372/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010373{
10374 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010375 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010376
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010377 do {
10378 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +100010379#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010380 if (dir_fd != DEFAULT_DIR_FD)
10381 result = mkfifoat(dir_fd, path->narrow, mode);
10382 else
Ross Lagerwall7807c352011-03-17 20:20:30 +020010383#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010384 result = mkfifo(path->narrow, mode);
10385 Py_END_ALLOW_THREADS
10386 } while (result != 0 && errno == EINTR &&
10387 !(async_err = PyErr_CheckSignals()));
10388 if (result != 0)
10389 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010390
10391 Py_RETURN_NONE;
10392}
10393#endif /* HAVE_MKFIFO */
10394
10395
10396#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
10397/*[clinic input]
10398os.mknod
10399
10400 path: path_t
10401 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010402 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +100010403 *
10404 dir_fd: dir_fd(requires='mknodat')=None
10405
10406Create a node in the file system.
10407
10408Create a node in the file system (file, device special file or named pipe)
10409at path. mode specifies both the permissions to use and the
10410type of node to be created, being combined (bitwise OR) with one of
10411S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
10412device defines the newly created device special file (probably using
10413os.makedev()). Otherwise device is ignored.
10414
10415If dir_fd is not None, it should be a file descriptor open to a directory,
10416 and path should be relative; path will then be relative to that directory.
10417dir_fd may not be implemented on your platform.
10418 If it is unavailable, using it will raise a NotImplementedError.
10419[clinic start generated code]*/
10420
Larry Hastings2f936352014-08-05 14:04:04 +100010421static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010422os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -040010423 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010424/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010425{
10426 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010427 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010428
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010429 do {
10430 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +100010431#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010432 if (dir_fd != DEFAULT_DIR_FD)
10433 result = mknodat(dir_fd, path->narrow, mode, device);
10434 else
Larry Hastings2f936352014-08-05 14:04:04 +100010435#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010436 result = mknod(path->narrow, mode, device);
10437 Py_END_ALLOW_THREADS
10438 } while (result != 0 && errno == EINTR &&
10439 !(async_err = PyErr_CheckSignals()));
10440 if (result != 0)
10441 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010442
10443 Py_RETURN_NONE;
10444}
10445#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
10446
10447
10448#ifdef HAVE_DEVICE_MACROS
10449/*[clinic input]
10450os.major -> unsigned_int
10451
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010452 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010453 /
10454
10455Extracts a device major number from a raw device number.
10456[clinic start generated code]*/
10457
Larry Hastings2f936352014-08-05 14:04:04 +100010458static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010459os_major_impl(PyObject *module, dev_t device)
10460/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010461{
10462 return major(device);
10463}
10464
10465
10466/*[clinic input]
10467os.minor -> unsigned_int
10468
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010469 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010470 /
10471
10472Extracts a device minor number from a raw device number.
10473[clinic start generated code]*/
10474
Larry Hastings2f936352014-08-05 14:04:04 +100010475static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010476os_minor_impl(PyObject *module, dev_t device)
10477/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010478{
10479 return minor(device);
10480}
10481
10482
10483/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010484os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010485
10486 major: int
10487 minor: int
10488 /
10489
10490Composes a raw device number from the major and minor device numbers.
10491[clinic start generated code]*/
10492
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010493static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010494os_makedev_impl(PyObject *module, int major, int minor)
10495/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010496{
10497 return makedev(major, minor);
10498}
10499#endif /* HAVE_DEVICE_MACROS */
10500
10501
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010502#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010503/*[clinic input]
10504os.ftruncate
10505
10506 fd: int
10507 length: Py_off_t
10508 /
10509
10510Truncate a file, specified by file descriptor, to a specific length.
10511[clinic start generated code]*/
10512
Larry Hastings2f936352014-08-05 14:04:04 +100010513static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010514os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
10515/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010516{
10517 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010518 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010519
Steve Dowerb82e17e2019-05-23 08:45:22 -070010520 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
10521 return NULL;
10522 }
10523
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010524 do {
10525 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010526 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010527#ifdef MS_WINDOWS
10528 result = _chsize_s(fd, length);
10529#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010530 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010531#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010532 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010533 Py_END_ALLOW_THREADS
10534 } while (result != 0 && errno == EINTR &&
10535 !(async_err = PyErr_CheckSignals()));
10536 if (result != 0)
10537 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010538 Py_RETURN_NONE;
10539}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010540#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010541
10542
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010543#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010544/*[clinic input]
10545os.truncate
10546 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
10547 length: Py_off_t
10548
10549Truncate a file, specified by path, to a specific length.
10550
10551On some platforms, path may also be specified as an open file descriptor.
10552 If this functionality is unavailable, using it raises an exception.
10553[clinic start generated code]*/
10554
Larry Hastings2f936352014-08-05 14:04:04 +100010555static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010556os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
10557/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010558{
10559 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010560#ifdef MS_WINDOWS
10561 int fd;
10562#endif
10563
10564 if (path->fd != -1)
10565 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010566
Steve Dowerb82e17e2019-05-23 08:45:22 -070010567 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
10568 return NULL;
10569 }
10570
Larry Hastings2f936352014-08-05 14:04:04 +100010571 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010572 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010573#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070010574 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +020010575 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010576 result = -1;
10577 else {
10578 result = _chsize_s(fd, length);
10579 close(fd);
10580 if (result < 0)
10581 errno = result;
10582 }
10583#else
10584 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010585#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010586 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +100010587 Py_END_ALLOW_THREADS
10588 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +030010589 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +100010590
10591 Py_RETURN_NONE;
10592}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010593#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010594
Ross Lagerwall7807c352011-03-17 20:20:30 +020010595
Victor Stinnerd6b17692014-09-30 12:20:05 +020010596/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
10597 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
10598 defined, which is the case in Python on AIX. AIX bug report:
10599 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
10600#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
10601# define POSIX_FADVISE_AIX_BUG
10602#endif
10603
Victor Stinnerec39e262014-09-30 12:35:58 +020010604
Victor Stinnerd6b17692014-09-30 12:20:05 +020010605#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010606/*[clinic input]
10607os.posix_fallocate
10608
10609 fd: int
10610 offset: Py_off_t
10611 length: Py_off_t
10612 /
10613
10614Ensure a file has allocated at least a particular number of bytes on disk.
10615
10616Ensure that the file specified by fd encompasses a range of bytes
10617starting at offset bytes from the beginning and continuing for length bytes.
10618[clinic start generated code]*/
10619
Larry Hastings2f936352014-08-05 14:04:04 +100010620static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010621os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010622 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010623/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010624{
10625 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010626 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010627
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010628 do {
10629 Py_BEGIN_ALLOW_THREADS
10630 result = posix_fallocate(fd, offset, length);
10631 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010632 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10633
10634 if (result == 0)
10635 Py_RETURN_NONE;
10636
10637 if (async_err)
10638 return NULL;
10639
10640 errno = result;
10641 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010642}
Victor Stinnerec39e262014-09-30 12:35:58 +020010643#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +100010644
Ross Lagerwall7807c352011-03-17 20:20:30 +020010645
Victor Stinnerd6b17692014-09-30 12:20:05 +020010646#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010647/*[clinic input]
10648os.posix_fadvise
10649
10650 fd: int
10651 offset: Py_off_t
10652 length: Py_off_t
10653 advice: int
10654 /
10655
10656Announce an intention to access data in a specific pattern.
10657
10658Announce an intention to access data in a specific pattern, thus allowing
10659the kernel to make optimizations.
10660The advice applies to the region of the file specified by fd starting at
10661offset and continuing for length bytes.
10662advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10663POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10664POSIX_FADV_DONTNEED.
10665[clinic start generated code]*/
10666
Larry Hastings2f936352014-08-05 14:04:04 +100010667static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010668os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010669 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010670/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010671{
10672 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010673 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010674
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010675 do {
10676 Py_BEGIN_ALLOW_THREADS
10677 result = posix_fadvise(fd, offset, length, advice);
10678 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010679 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10680
10681 if (result == 0)
10682 Py_RETURN_NONE;
10683
10684 if (async_err)
10685 return NULL;
10686
10687 errno = result;
10688 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010689}
Victor Stinnerec39e262014-09-30 12:35:58 +020010690#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010691
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010692
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010693#ifdef MS_WINDOWS
Victor Stinner161e7b32020-01-24 11:53:44 +010010694static PyObject*
10695win32_putenv(PyObject *name, PyObject *value)
10696{
10697 /* Search from index 1 because on Windows starting '=' is allowed for
10698 defining hidden environment variables. */
10699 if (PyUnicode_GET_LENGTH(name) == 0 ||
10700 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10701 {
10702 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10703 return NULL;
10704 }
10705 PyObject *unicode;
10706 if (value != NULL) {
10707 unicode = PyUnicode_FromFormat("%U=%U", name, value);
10708 }
10709 else {
10710 unicode = PyUnicode_FromFormat("%U=", name);
10711 }
10712 if (unicode == NULL) {
10713 return NULL;
10714 }
10715
10716 Py_ssize_t size;
10717 /* PyUnicode_AsWideCharString() rejects embedded null characters */
10718 wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
10719 Py_DECREF(unicode);
10720
10721 if (env == NULL) {
10722 return NULL;
10723 }
10724 if (size > _MAX_ENV) {
10725 PyErr_Format(PyExc_ValueError,
10726 "the environment variable is longer than %u characters",
10727 _MAX_ENV);
10728 PyMem_Free(env);
10729 return NULL;
10730 }
10731
10732 /* _wputenv() and SetEnvironmentVariableW() update the environment in the
10733 Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
10734 and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
10735
10736 Prefer _wputenv() to be compatible with C libraries using CRT
10737 variables and CRT functions using these variables (ex: getenv()). */
10738 int err = _wputenv(env);
10739 PyMem_Free(env);
10740
10741 if (err) {
10742 posix_error();
10743 return NULL;
10744 }
10745
10746 Py_RETURN_NONE;
10747}
10748#endif
10749
10750
10751#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010752/*[clinic input]
10753os.putenv
10754
10755 name: unicode
10756 value: unicode
10757 /
10758
10759Change or add an environment variable.
10760[clinic start generated code]*/
10761
Larry Hastings2f936352014-08-05 14:04:04 +100010762static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010763os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10764/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010765{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010766 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10767 return NULL;
10768 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010769 return win32_putenv(name, value);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010770}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010771#else
Larry Hastings2f936352014-08-05 14:04:04 +100010772/*[clinic input]
10773os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010774
Larry Hastings2f936352014-08-05 14:04:04 +100010775 name: FSConverter
10776 value: FSConverter
10777 /
10778
10779Change or add an environment variable.
10780[clinic start generated code]*/
10781
Larry Hastings2f936352014-08-05 14:04:04 +100010782static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010783os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10784/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010785{
Serhiy Storchaka77703942017-06-25 07:33:01 +030010786 const char *name_string = PyBytes_AS_STRING(name);
10787 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010788
Serhiy Storchaka77703942017-06-25 07:33:01 +030010789 if (strchr(name_string, '=') != NULL) {
10790 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10791 return NULL;
10792 }
Victor Stinnerb477d192020-01-22 22:48:16 +010010793
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010794 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10795 return NULL;
10796 }
10797
Victor Stinnerb477d192020-01-22 22:48:16 +010010798 if (setenv(name_string, value_string, 1)) {
10799 return posix_error();
10800 }
Larry Hastings2f936352014-08-05 14:04:04 +100010801 Py_RETURN_NONE;
10802}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010803#endif /* !defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100010804
10805
Victor Stinner161e7b32020-01-24 11:53:44 +010010806#ifdef MS_WINDOWS
10807/*[clinic input]
10808os.unsetenv
10809 name: unicode
10810 /
10811
10812Delete an environment variable.
10813[clinic start generated code]*/
10814
10815static PyObject *
10816os_unsetenv_impl(PyObject *module, PyObject *name)
10817/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10818{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010819 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10820 return NULL;
10821 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010822 return win32_putenv(name, NULL);
10823}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010824#else
Larry Hastings2f936352014-08-05 14:04:04 +100010825/*[clinic input]
10826os.unsetenv
10827 name: FSConverter
10828 /
10829
10830Delete an environment variable.
10831[clinic start generated code]*/
10832
Larry Hastings2f936352014-08-05 14:04:04 +100010833static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010834os_unsetenv_impl(PyObject *module, PyObject *name)
10835/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010836{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010837 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10838 return NULL;
10839 }
Victor Stinner984890f2011-11-24 13:53:38 +010010840#ifdef HAVE_BROKEN_UNSETENV
10841 unsetenv(PyBytes_AS_STRING(name));
10842#else
Victor Stinner161e7b32020-01-24 11:53:44 +010010843 int err = unsetenv(PyBytes_AS_STRING(name));
10844 if (err) {
Victor Stinner60b385e2011-11-22 22:01:28 +010010845 return posix_error();
Victor Stinner161e7b32020-01-24 11:53:44 +010010846 }
Victor Stinner984890f2011-11-24 13:53:38 +010010847#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010848
Victor Stinner84ae1182010-05-06 22:05:07 +000010849 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010850}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010851#endif /* !MS_WINDOWS */
Guido van Rossumc524d952001-10-19 01:31:59 +000010852
Larry Hastings2f936352014-08-05 14:04:04 +100010853
10854/*[clinic input]
10855os.strerror
10856
10857 code: int
10858 /
10859
10860Translate an error code to a message string.
10861[clinic start generated code]*/
10862
Larry Hastings2f936352014-08-05 14:04:04 +100010863static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010864os_strerror_impl(PyObject *module, int code)
10865/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010866{
10867 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010868 if (message == NULL) {
10869 PyErr_SetString(PyExc_ValueError,
10870 "strerror() argument out of range");
10871 return NULL;
10872 }
Victor Stinner1b579672011-12-17 05:47:23 +010010873 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010874}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010875
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010876
Guido van Rossumc9641791998-08-04 15:26:23 +000010877#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010878#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010879/*[clinic input]
10880os.WCOREDUMP -> bool
10881
10882 status: int
10883 /
10884
10885Return True if the process returning status was dumped to a core file.
10886[clinic start generated code]*/
10887
Larry Hastings2f936352014-08-05 14:04:04 +100010888static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010889os_WCOREDUMP_impl(PyObject *module, int status)
10890/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010891{
10892 WAIT_TYPE wait_status;
10893 WAIT_STATUS_INT(wait_status) = status;
10894 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010895}
10896#endif /* WCOREDUMP */
10897
Larry Hastings2f936352014-08-05 14:04:04 +100010898
Fred Drake106c1a02002-04-23 15:58:02 +000010899#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010900/*[clinic input]
10901os.WIFCONTINUED -> bool
10902
10903 status: int
10904
10905Return True if a particular process was continued from a job control stop.
10906
10907Return True if the process returning status was continued from a
10908job control stop.
10909[clinic start generated code]*/
10910
Larry Hastings2f936352014-08-05 14:04:04 +100010911static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010912os_WIFCONTINUED_impl(PyObject *module, int status)
10913/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010914{
10915 WAIT_TYPE wait_status;
10916 WAIT_STATUS_INT(wait_status) = status;
10917 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010918}
10919#endif /* WIFCONTINUED */
10920
Larry Hastings2f936352014-08-05 14:04:04 +100010921
Guido van Rossumc9641791998-08-04 15:26:23 +000010922#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010923/*[clinic input]
10924os.WIFSTOPPED -> bool
10925
10926 status: int
10927
10928Return True if the process returning status was stopped.
10929[clinic start generated code]*/
10930
Larry Hastings2f936352014-08-05 14:04:04 +100010931static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010932os_WIFSTOPPED_impl(PyObject *module, int status)
10933/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010934{
10935 WAIT_TYPE wait_status;
10936 WAIT_STATUS_INT(wait_status) = status;
10937 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010938}
10939#endif /* WIFSTOPPED */
10940
Larry Hastings2f936352014-08-05 14:04:04 +100010941
Guido van Rossumc9641791998-08-04 15:26:23 +000010942#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010943/*[clinic input]
10944os.WIFSIGNALED -> bool
10945
10946 status: int
10947
10948Return True if the process returning status was terminated by a signal.
10949[clinic start generated code]*/
10950
Larry Hastings2f936352014-08-05 14:04:04 +100010951static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010952os_WIFSIGNALED_impl(PyObject *module, int status)
10953/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010954{
10955 WAIT_TYPE wait_status;
10956 WAIT_STATUS_INT(wait_status) = status;
10957 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010958}
10959#endif /* WIFSIGNALED */
10960
Larry Hastings2f936352014-08-05 14:04:04 +100010961
Guido van Rossumc9641791998-08-04 15:26:23 +000010962#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010963/*[clinic input]
10964os.WIFEXITED -> bool
10965
10966 status: int
10967
10968Return True if the process returning status exited via the exit() system call.
10969[clinic start generated code]*/
10970
Larry Hastings2f936352014-08-05 14:04:04 +100010971static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010972os_WIFEXITED_impl(PyObject *module, int status)
10973/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010974{
10975 WAIT_TYPE wait_status;
10976 WAIT_STATUS_INT(wait_status) = status;
10977 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010978}
10979#endif /* WIFEXITED */
10980
Larry Hastings2f936352014-08-05 14:04:04 +100010981
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010982#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010983/*[clinic input]
10984os.WEXITSTATUS -> int
10985
10986 status: int
10987
10988Return the process return code from status.
10989[clinic start generated code]*/
10990
Larry Hastings2f936352014-08-05 14:04:04 +100010991static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010992os_WEXITSTATUS_impl(PyObject *module, int status)
10993/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010994{
10995 WAIT_TYPE wait_status;
10996 WAIT_STATUS_INT(wait_status) = status;
10997 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010998}
10999#endif /* WEXITSTATUS */
11000
Larry Hastings2f936352014-08-05 14:04:04 +100011001
Guido van Rossumc9641791998-08-04 15:26:23 +000011002#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100011003/*[clinic input]
11004os.WTERMSIG -> int
11005
11006 status: int
11007
11008Return the signal that terminated the process that provided the status value.
11009[clinic start generated code]*/
11010
Larry Hastings2f936352014-08-05 14:04:04 +100011011static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011012os_WTERMSIG_impl(PyObject *module, int status)
11013/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011014{
11015 WAIT_TYPE wait_status;
11016 WAIT_STATUS_INT(wait_status) = status;
11017 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011018}
11019#endif /* WTERMSIG */
11020
Larry Hastings2f936352014-08-05 14:04:04 +100011021
Guido van Rossumc9641791998-08-04 15:26:23 +000011022#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100011023/*[clinic input]
11024os.WSTOPSIG -> int
11025
11026 status: int
11027
11028Return the signal that stopped the process that provided the status value.
11029[clinic start generated code]*/
11030
Larry Hastings2f936352014-08-05 14:04:04 +100011031static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011032os_WSTOPSIG_impl(PyObject *module, int status)
11033/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011034{
11035 WAIT_TYPE wait_status;
11036 WAIT_STATUS_INT(wait_status) = status;
11037 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000011038}
11039#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000011040#endif /* HAVE_SYS_WAIT_H */
11041
11042
Thomas Wouters477c8d52006-05-27 19:21:47 +000011043#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000011044#ifdef _SCO_DS
11045/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
11046 needed definitions in sys/statvfs.h */
11047#define _SVID3
11048#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000011049#include <sys/statvfs.h>
11050
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011051static PyObject*
Victor Stinner1c2fa782020-05-10 11:05:29 +020011052_pystatvfs_fromstructstatvfs(PyObject *module, struct statvfs st) {
11053 PyObject *StatVFSResultType = get_posix_state(module)->StatVFSResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080011054 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000011055 if (v == NULL)
11056 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011057
11058#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000011059 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
11060 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
11061 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
11062 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
11063 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
11064 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
11065 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
11066 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
11067 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
11068 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011069#else
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
11071 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
11072 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011073 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000011074 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011075 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011077 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000011078 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011079 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000011080 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011081 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070011083 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000011084 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
11085 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011086#endif
Michael Felt502d5512018-01-05 13:01:58 +010011087/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
11088 * (issue #32390). */
11089#if defined(_AIX) && defined(_ALL_SOURCE)
11090 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
11091#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010011092 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010011093#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010011094 if (PyErr_Occurred()) {
11095 Py_DECREF(v);
11096 return NULL;
11097 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011098
Victor Stinner8c62be82010-05-06 00:08:46 +000011099 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011100}
11101
Larry Hastings2f936352014-08-05 14:04:04 +100011102
11103/*[clinic input]
11104os.fstatvfs
11105 fd: int
11106 /
11107
11108Perform an fstatvfs system call on the given fd.
11109
11110Equivalent to statvfs(fd).
11111[clinic start generated code]*/
11112
Larry Hastings2f936352014-08-05 14:04:04 +100011113static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011114os_fstatvfs_impl(PyObject *module, int fd)
11115/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011116{
11117 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011118 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011119 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011120
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011121 do {
11122 Py_BEGIN_ALLOW_THREADS
11123 result = fstatvfs(fd, &st);
11124 Py_END_ALLOW_THREADS
11125 } while (result != 0 && errno == EINTR &&
11126 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100011127 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000011128 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000011129
Victor Stinner1c2fa782020-05-10 11:05:29 +020011130 return _pystatvfs_fromstructstatvfs(module, st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000011131}
Larry Hastings2f936352014-08-05 14:04:04 +100011132#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000011133
11134
Thomas Wouters477c8d52006-05-27 19:21:47 +000011135#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000011136#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100011137/*[clinic input]
11138os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000011139
Larry Hastings2f936352014-08-05 14:04:04 +100011140 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
11141
11142Perform a statvfs system call on the given path.
11143
11144path may always be specified as a string.
11145On some platforms, path may also be specified as an open file descriptor.
11146 If this functionality is unavailable, using it raises an exception.
11147[clinic start generated code]*/
11148
Larry Hastings2f936352014-08-05 14:04:04 +100011149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011150os_statvfs_impl(PyObject *module, path_t *path)
11151/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011152{
11153 int result;
11154 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070011155
11156 Py_BEGIN_ALLOW_THREADS
11157#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100011158 if (path->fd != -1) {
Larry Hastings2f936352014-08-05 14:04:04 +100011159 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011160 }
11161 else
11162#endif
Larry Hastings2f936352014-08-05 14:04:04 +100011163 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011164 Py_END_ALLOW_THREADS
11165
11166 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100011167 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070011168 }
11169
Victor Stinner1c2fa782020-05-10 11:05:29 +020011170 return _pystatvfs_fromstructstatvfs(module, st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000011171}
Larry Hastings2f936352014-08-05 14:04:04 +100011172#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
11173
Guido van Rossum94f6f721999-01-06 18:42:14 +000011174
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011175#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100011176/*[clinic input]
11177os._getdiskusage
11178
Steve Dower23ad6d02018-02-22 10:39:10 -080011179 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100011180
11181Return disk usage statistics about the given path as a (total, free) tuple.
11182[clinic start generated code]*/
11183
Larry Hastings2f936352014-08-05 14:04:04 +100011184static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080011185os__getdiskusage_impl(PyObject *module, path_t *path)
11186/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011187{
11188 BOOL retval;
11189 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040011190 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011191
11192 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080011193 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011194 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040011195 if (retval == 0) {
11196 if (GetLastError() == ERROR_DIRECTORY) {
11197 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011198
Joe Pamerc8c02492018-09-25 10:57:36 -040011199 dir_path = PyMem_New(wchar_t, path->length + 1);
11200 if (dir_path == NULL) {
11201 return PyErr_NoMemory();
11202 }
11203
11204 wcscpy_s(dir_path, path->length + 1, path->wide);
11205
11206 if (_dirnameW(dir_path) != -1) {
11207 Py_BEGIN_ALLOW_THREADS
11208 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
11209 Py_END_ALLOW_THREADS
11210 }
11211 /* Record the last error in case it's modified by PyMem_Free. */
11212 err = GetLastError();
11213 PyMem_Free(dir_path);
11214 if (retval) {
11215 goto success;
11216 }
11217 }
11218 return PyErr_SetFromWindowsErr(err);
11219 }
11220
11221success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011222 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
11223}
Larry Hastings2f936352014-08-05 14:04:04 +100011224#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020011225
11226
Fred Drakec9680921999-12-13 16:37:25 +000011227/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
11228 * It maps strings representing configuration variable names to
11229 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000011230 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000011231 * rarely-used constants. There are three separate tables that use
11232 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000011233 *
11234 * This code is always included, even if none of the interfaces that
11235 * need it are included. The #if hackery needed to avoid it would be
11236 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000011237 */
11238struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020011239 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030011240 int value;
Fred Drakec9680921999-12-13 16:37:25 +000011241};
11242
Fred Drake12c6e2d1999-12-14 21:25:03 +000011243static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011244conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000011245 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000011246{
Christian Heimes217cfd12007-12-02 14:31:20 +000011247 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030011248 int value = _PyLong_AsInt(arg);
11249 if (value == -1 && PyErr_Occurred())
11250 return 0;
11251 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000011252 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000011253 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000011254 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000011255 /* look up the value in the table using a binary search */
11256 size_t lo = 0;
11257 size_t mid;
11258 size_t hi = tablesize;
11259 int cmp;
11260 const char *confname;
11261 if (!PyUnicode_Check(arg)) {
11262 PyErr_SetString(PyExc_TypeError,
11263 "configuration names must be strings or integers");
11264 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011265 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020011266 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000011267 if (confname == NULL)
11268 return 0;
11269 while (lo < hi) {
11270 mid = (lo + hi) / 2;
11271 cmp = strcmp(confname, table[mid].name);
11272 if (cmp < 0)
11273 hi = mid;
11274 else if (cmp > 0)
11275 lo = mid + 1;
11276 else {
11277 *valuep = table[mid].value;
11278 return 1;
11279 }
11280 }
11281 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
11282 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000011283 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000011284}
11285
11286
11287#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
11288static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000011289#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011290 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011291#endif
11292#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011293 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011294#endif
Fred Drakec9680921999-12-13 16:37:25 +000011295#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011296 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011297#endif
11298#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000011299 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000011300#endif
11301#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000011302 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000011303#endif
11304#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000011305 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000011306#endif
11307#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011308 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011309#endif
11310#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000011311 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000011312#endif
11313#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000011314 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000011315#endif
11316#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011317 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011318#endif
11319#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011320 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000011321#endif
11322#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011323 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011324#endif
11325#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000011326 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000011327#endif
11328#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011329 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011330#endif
11331#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000011332 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000011333#endif
11334#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011335 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011336#endif
11337#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000011338 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000011339#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000011340#ifdef _PC_ACL_ENABLED
11341 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
11342#endif
11343#ifdef _PC_MIN_HOLE_SIZE
11344 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
11345#endif
11346#ifdef _PC_ALLOC_SIZE_MIN
11347 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
11348#endif
11349#ifdef _PC_REC_INCR_XFER_SIZE
11350 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
11351#endif
11352#ifdef _PC_REC_MAX_XFER_SIZE
11353 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
11354#endif
11355#ifdef _PC_REC_MIN_XFER_SIZE
11356 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
11357#endif
11358#ifdef _PC_REC_XFER_ALIGN
11359 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
11360#endif
11361#ifdef _PC_SYMLINK_MAX
11362 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
11363#endif
11364#ifdef _PC_XATTR_ENABLED
11365 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
11366#endif
11367#ifdef _PC_XATTR_EXISTS
11368 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
11369#endif
11370#ifdef _PC_TIMESTAMP_RESOLUTION
11371 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
11372#endif
Fred Drakec9680921999-12-13 16:37:25 +000011373};
11374
Fred Drakec9680921999-12-13 16:37:25 +000011375static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011376conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011377{
11378 return conv_confname(arg, valuep, posix_constants_pathconf,
11379 sizeof(posix_constants_pathconf)
11380 / sizeof(struct constdef));
11381}
11382#endif
11383
Larry Hastings2f936352014-08-05 14:04:04 +100011384
Fred Drakec9680921999-12-13 16:37:25 +000011385#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011386/*[clinic input]
11387os.fpathconf -> long
11388
Gregory P. Smith3ccb96c2020-06-20 15:06:48 -070011389 fd: fildes
Larry Hastings2f936352014-08-05 14:04:04 +100011390 name: path_confname
11391 /
11392
11393Return the configuration limit name for the file descriptor fd.
11394
11395If there is no limit, return -1.
11396[clinic start generated code]*/
11397
Larry Hastings2f936352014-08-05 14:04:04 +100011398static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011399os_fpathconf_impl(PyObject *module, int fd, int name)
Gregory P. Smith3ccb96c2020-06-20 15:06:48 -070011400/*[clinic end generated code: output=d5b7042425fc3e21 input=5b8d2471cfaae186]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011401{
11402 long limit;
11403
11404 errno = 0;
11405 limit = fpathconf(fd, name);
11406 if (limit == -1 && errno != 0)
11407 posix_error();
11408
11409 return limit;
11410}
11411#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011412
11413
11414#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011415/*[clinic input]
11416os.pathconf -> long
11417 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
11418 name: path_confname
11419
11420Return the configuration limit name for the file or directory path.
11421
11422If there is no limit, return -1.
11423On some platforms, path may also be specified as an open file descriptor.
11424 If this functionality is unavailable, using it raises an exception.
11425[clinic start generated code]*/
11426
Larry Hastings2f936352014-08-05 14:04:04 +100011427static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011428os_pathconf_impl(PyObject *module, path_t *path, int name)
11429/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011430{
Victor Stinner8c62be82010-05-06 00:08:46 +000011431 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000011432
Victor Stinner8c62be82010-05-06 00:08:46 +000011433 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020011434#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100011435 if (path->fd != -1)
11436 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020011437 else
11438#endif
Larry Hastings2f936352014-08-05 14:04:04 +100011439 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000011440 if (limit == -1 && errno != 0) {
11441 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000011442 /* could be a path or name problem */
11443 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000011444 else
Larry Hastings2f936352014-08-05 14:04:04 +100011445 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000011446 }
Larry Hastings2f936352014-08-05 14:04:04 +100011447
11448 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000011449}
Larry Hastings2f936352014-08-05 14:04:04 +100011450#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011451
11452#ifdef HAVE_CONFSTR
11453static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000011454#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000011455 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000011456#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000011457#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011458 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011459#endif
11460#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011461 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011462#endif
Fred Draked86ed291999-12-15 15:34:33 +000011463#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011464 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011465#endif
11466#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011467 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011468#endif
11469#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011470 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011471#endif
11472#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011473 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011474#endif
Fred Drakec9680921999-12-13 16:37:25 +000011475#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011476 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011477#endif
11478#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011479 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011480#endif
11481#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011482 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011483#endif
11484#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011485 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011486#endif
11487#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011488 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011489#endif
11490#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011491 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011492#endif
11493#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011494 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011495#endif
11496#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011497 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011498#endif
Fred Draked86ed291999-12-15 15:34:33 +000011499#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000011500 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000011501#endif
Fred Drakec9680921999-12-13 16:37:25 +000011502#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000011503 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000011504#endif
Fred Draked86ed291999-12-15 15:34:33 +000011505#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011506 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000011507#endif
11508#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011509 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000011510#endif
11511#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011512 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011513#endif
11514#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011515 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000011516#endif
Fred Drakec9680921999-12-13 16:37:25 +000011517#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011518 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011519#endif
11520#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011521 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011522#endif
11523#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011524 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011525#endif
11526#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011527 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011528#endif
11529#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011530 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011531#endif
11532#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011533 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011534#endif
11535#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011536 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011537#endif
11538#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011539 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011540#endif
11541#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011542 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011543#endif
11544#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011545 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011546#endif
11547#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011548 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011549#endif
11550#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011551 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011552#endif
11553#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011554 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011555#endif
11556#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011557 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011558#endif
11559#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011560 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011561#endif
11562#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011563 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011564#endif
Fred Draked86ed291999-12-15 15:34:33 +000011565#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011566 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011567#endif
11568#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011569 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000011570#endif
11571#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000011572 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000011573#endif
11574#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011575 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011576#endif
11577#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011578 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011579#endif
11580#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000011581 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000011582#endif
11583#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011584 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000011585#endif
11586#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000011587 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000011588#endif
11589#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011590 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011591#endif
11592#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011593 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011594#endif
11595#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011596 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011597#endif
11598#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011599 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011600#endif
11601#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000011602 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000011603#endif
Fred Drakec9680921999-12-13 16:37:25 +000011604};
11605
11606static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011607conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011608{
11609 return conv_confname(arg, valuep, posix_constants_confstr,
11610 sizeof(posix_constants_confstr)
11611 / sizeof(struct constdef));
11612}
11613
Larry Hastings2f936352014-08-05 14:04:04 +100011614
11615/*[clinic input]
11616os.confstr
11617
11618 name: confstr_confname
11619 /
11620
11621Return a string-valued system configuration variable.
11622[clinic start generated code]*/
11623
Larry Hastings2f936352014-08-05 14:04:04 +100011624static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011625os_confstr_impl(PyObject *module, int name)
11626/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000011627{
11628 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000011629 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011630 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000011631
Victor Stinnercb043522010-09-10 23:49:04 +000011632 errno = 0;
11633 len = confstr(name, buffer, sizeof(buffer));
11634 if (len == 0) {
11635 if (errno) {
11636 posix_error();
11637 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000011638 }
11639 else {
Victor Stinnercb043522010-09-10 23:49:04 +000011640 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000011641 }
11642 }
Victor Stinnercb043522010-09-10 23:49:04 +000011643
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011644 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011645 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011646 char *buf = PyMem_Malloc(len);
11647 if (buf == NULL)
11648 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011649 len2 = confstr(name, buf, len);
11650 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011651 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011652 PyMem_Free(buf);
11653 }
11654 else
11655 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011656 return result;
11657}
Larry Hastings2f936352014-08-05 14:04:04 +100011658#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011659
11660
11661#ifdef HAVE_SYSCONF
11662static struct constdef posix_constants_sysconf[] = {
11663#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011664 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011665#endif
11666#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011667 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011668#endif
11669#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011670 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011671#endif
11672#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011673 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011674#endif
11675#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011676 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011677#endif
11678#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011679 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011680#endif
11681#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011682 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011683#endif
11684#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011685 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011686#endif
11687#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011688 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011689#endif
11690#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011691 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011692#endif
Fred Draked86ed291999-12-15 15:34:33 +000011693#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011694 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011695#endif
11696#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011697 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011698#endif
Fred Drakec9680921999-12-13 16:37:25 +000011699#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011700 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011701#endif
Fred Drakec9680921999-12-13 16:37:25 +000011702#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011703 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011704#endif
11705#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011706 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011707#endif
11708#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011709 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011710#endif
11711#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011712 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011713#endif
11714#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011715 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011716#endif
Fred Draked86ed291999-12-15 15:34:33 +000011717#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011718 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011719#endif
Fred Drakec9680921999-12-13 16:37:25 +000011720#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011721 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011722#endif
11723#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011724 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011725#endif
11726#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011727 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011728#endif
11729#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011730 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011731#endif
11732#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011733 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011734#endif
Fred Draked86ed291999-12-15 15:34:33 +000011735#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011736 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011737#endif
Fred Drakec9680921999-12-13 16:37:25 +000011738#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011739 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011740#endif
11741#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011742 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011743#endif
11744#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011745 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011746#endif
11747#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011748 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011749#endif
11750#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011751 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011752#endif
11753#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011754 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011755#endif
11756#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011757 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011758#endif
11759#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011760 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011761#endif
11762#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011763 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011764#endif
11765#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011766 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011767#endif
11768#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011769 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011770#endif
11771#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011772 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011773#endif
11774#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011775 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011776#endif
11777#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011778 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011779#endif
11780#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011781 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011782#endif
11783#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011784 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011785#endif
11786#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011787 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011788#endif
11789#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011790 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011791#endif
11792#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011793 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011794#endif
11795#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011796 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011797#endif
11798#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011799 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011800#endif
11801#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011802 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011803#endif
11804#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011805 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011806#endif
Fred Draked86ed291999-12-15 15:34:33 +000011807#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011808 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011809#endif
Fred Drakec9680921999-12-13 16:37:25 +000011810#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011811 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011812#endif
11813#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011814 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011815#endif
11816#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011817 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011818#endif
Fred Draked86ed291999-12-15 15:34:33 +000011819#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011820 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011821#endif
Fred Drakec9680921999-12-13 16:37:25 +000011822#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011823 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011824#endif
Fred Draked86ed291999-12-15 15:34:33 +000011825#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011826 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011827#endif
11828#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011829 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011830#endif
Fred Drakec9680921999-12-13 16:37:25 +000011831#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011832 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011833#endif
11834#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011835 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011836#endif
11837#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011838 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011839#endif
11840#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011841 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011842#endif
Fred Draked86ed291999-12-15 15:34:33 +000011843#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011844 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011845#endif
Fred Drakec9680921999-12-13 16:37:25 +000011846#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011847 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011848#endif
11849#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011850 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011851#endif
11852#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011853 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011854#endif
11855#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011856 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011857#endif
11858#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011859 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011860#endif
11861#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011862 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011863#endif
11864#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011865 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011866#endif
Fred Draked86ed291999-12-15 15:34:33 +000011867#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011868 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011869#endif
Fred Drakec9680921999-12-13 16:37:25 +000011870#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011871 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011872#endif
11873#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011874 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011875#endif
Fred Draked86ed291999-12-15 15:34:33 +000011876#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011877 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011878#endif
Fred Drakec9680921999-12-13 16:37:25 +000011879#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011880 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011881#endif
11882#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011883 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011884#endif
11885#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011886 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011887#endif
11888#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011889 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011890#endif
11891#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011892 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011893#endif
11894#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011895 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011896#endif
11897#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011898 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011899#endif
11900#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011901 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011902#endif
11903#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011904 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011905#endif
Fred Draked86ed291999-12-15 15:34:33 +000011906#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011907 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011908#endif
11909#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011910 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011911#endif
Fred Drakec9680921999-12-13 16:37:25 +000011912#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011913 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011914#endif
11915#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011916 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011917#endif
11918#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011919 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011920#endif
11921#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011922 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011923#endif
Batuhan Taşkaya909f4a32020-04-05 03:40:49 +030011924#ifdef _SC_AIX_REALMEM
11925 {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
11926#endif
Fred Drakec9680921999-12-13 16:37:25 +000011927#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011928 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011929#endif
11930#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011931 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011932#endif
11933#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011934 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011935#endif
11936#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011937 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011938#endif
11939#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011940 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011941#endif
11942#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011943 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011944#endif
11945#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011946 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011947#endif
11948#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011949 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011950#endif
11951#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011952 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011953#endif
11954#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011955 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011956#endif
11957#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011958 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011959#endif
11960#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011961 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011962#endif
11963#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011964 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011965#endif
11966#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011967 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011968#endif
11969#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011970 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011971#endif
11972#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011973 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011974#endif
11975#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011976 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011977#endif
11978#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011979 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011980#endif
11981#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011982 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011983#endif
11984#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011985 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011986#endif
11987#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011988 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011989#endif
11990#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011991 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011992#endif
11993#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011994 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011995#endif
11996#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011997 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011998#endif
11999#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012000 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012001#endif
12002#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000012003 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000012004#endif
12005#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012006 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012007#endif
12008#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012009 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012010#endif
12011#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012012 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012013#endif
12014#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012015 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012016#endif
12017#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012018 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012019#endif
Fred Draked86ed291999-12-15 15:34:33 +000012020#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000012021 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000012022#endif
Fred Drakec9680921999-12-13 16:37:25 +000012023#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000012024 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000012025#endif
12026#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012027 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012028#endif
12029#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000012030 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000012031#endif
12032#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012033 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012034#endif
12035#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000012036 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000012037#endif
12038#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000012039 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000012040#endif
12041#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000012042 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000012043#endif
12044#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000012045 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000012046#endif
12047#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000012048 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000012049#endif
12050#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012051 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012052#endif
12053#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000012054 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000012055#endif
12056#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012057 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000012058#endif
12059#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000012060 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000012061#endif
12062#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000012063 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000012064#endif
12065#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000012066 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000012067#endif
12068#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000012069 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000012070#endif
12071#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012072 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012073#endif
12074#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000012075 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000012076#endif
12077#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012078 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012079#endif
12080#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012081 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012082#endif
12083#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012084 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012085#endif
12086#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012087 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012088#endif
12089#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012090 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012091#endif
12092#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012093 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012094#endif
12095#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000012096 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000012097#endif
12098#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012099 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012100#endif
12101#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000012102 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000012103#endif
12104#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012105 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012106#endif
12107#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000012108 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000012109#endif
12110#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000012111 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000012112#endif
12113#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000012114 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000012115#endif
12116#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000012117 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000012118#endif
12119#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000012120 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000012121#endif
12122#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000012123 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000012124#endif
12125#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000012126 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000012127#endif
12128#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000012129 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000012130#endif
12131#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000012132 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000012133#endif
12134#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000012135 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000012136#endif
12137#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000012138 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000012139#endif
12140#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000012141 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000012142#endif
12143#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012144 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012145#endif
12146#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000012147 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000012148#endif
12149#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000012150 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000012151#endif
12152#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000012153 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000012154#endif
12155#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000012156 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000012157#endif
12158};
12159
12160static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012161conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000012162{
12163 return conv_confname(arg, valuep, posix_constants_sysconf,
12164 sizeof(posix_constants_sysconf)
12165 / sizeof(struct constdef));
12166}
12167
Larry Hastings2f936352014-08-05 14:04:04 +100012168
12169/*[clinic input]
12170os.sysconf -> long
12171 name: sysconf_confname
12172 /
12173
12174Return an integer-valued system configuration variable.
12175[clinic start generated code]*/
12176
Larry Hastings2f936352014-08-05 14:04:04 +100012177static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012178os_sysconf_impl(PyObject *module, int name)
12179/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012180{
12181 long value;
12182
12183 errno = 0;
12184 value = sysconf(name);
12185 if (value == -1 && errno != 0)
12186 posix_error();
12187 return value;
12188}
12189#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000012190
12191
Fred Drakebec628d1999-12-15 18:31:10 +000012192/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020012193 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000012194 * the exported dictionaries that are used to publish information about the
12195 * names available on the host platform.
12196 *
12197 * Sorting the table at runtime ensures that the table is properly ordered
12198 * when used, even for platforms we're not able to test on. It also makes
12199 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000012200 */
Fred Drakebec628d1999-12-15 18:31:10 +000012201
12202static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012203cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000012204{
12205 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000012206 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000012207 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000012208 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000012209
12210 return strcmp(c1->name, c2->name);
12211}
12212
12213static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000012214setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012215 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000012216{
Fred Drakebec628d1999-12-15 18:31:10 +000012217 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000012218 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000012219
12220 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
12221 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000012222 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000012223 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012224
Barry Warsaw3155db32000-04-13 15:20:40 +000012225 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000012226 PyObject *o = PyLong_FromLong(table[i].value);
12227 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
12228 Py_XDECREF(o);
12229 Py_DECREF(d);
12230 return -1;
12231 }
12232 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000012233 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000012234 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000012235}
12236
Fred Drakebec628d1999-12-15 18:31:10 +000012237/* Return -1 on failure, 0 on success. */
12238static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000012239setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000012240{
12241#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000012242 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000012243 sizeof(posix_constants_pathconf)
12244 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012245 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012246 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012247#endif
12248#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000012249 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000012250 sizeof(posix_constants_confstr)
12251 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012252 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012253 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012254#endif
12255#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000012256 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000012257 sizeof(posix_constants_sysconf)
12258 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000012259 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000012260 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000012261#endif
Fred Drakebec628d1999-12-15 18:31:10 +000012262 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000012263}
Fred Draked86ed291999-12-15 15:34:33 +000012264
12265
Larry Hastings2f936352014-08-05 14:04:04 +100012266/*[clinic input]
12267os.abort
12268
12269Abort the interpreter immediately.
12270
12271This function 'dumps core' or otherwise fails in the hardest way possible
12272on the hosting operating system. This function never returns.
12273[clinic start generated code]*/
12274
Larry Hastings2f936352014-08-05 14:04:04 +100012275static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012276os_abort_impl(PyObject *module)
12277/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012278{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012279 abort();
12280 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010012281#ifndef __clang__
12282 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
12283 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
12284 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012285 Py_FatalError("abort() called from Python code didn't abort!");
12286 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010012287#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012288}
Fred Drakebec628d1999-12-15 18:31:10 +000012289
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000012290#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080012291/* Grab ShellExecute dynamically from shell32 */
12292static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012293static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
12294 LPCWSTR, INT);
12295static int
12296check_ShellExecute()
12297{
12298 HINSTANCE hShell32;
12299
12300 /* only recheck */
12301 if (-1 == has_ShellExecute) {
12302 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070012303 /* Security note: this call is not vulnerable to "DLL hijacking".
12304 SHELL32 is part of "KnownDLLs" and so Windows always load
12305 the system SHELL32.DLL, even if there is another SHELL32.DLL
12306 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080012307 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080012308 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080012309 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
12310 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070012311 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012312 } else {
12313 has_ShellExecute = 0;
12314 }
Tony Roberts4860f012019-02-02 18:16:42 +010012315 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080012316 }
12317 return has_ShellExecute;
12318}
12319
12320
Steve Dowercc16be82016-09-08 10:35:16 -070012321/*[clinic input]
12322os.startfile
12323 filepath: path_t
12324 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000012325
Steve Dowercc16be82016-09-08 10:35:16 -070012326Start a file with its associated application.
12327
12328When "operation" is not specified or "open", this acts like
12329double-clicking the file in Explorer, or giving the file name as an
12330argument to the DOS "start" command: the file is opened with whatever
12331application (if any) its extension is associated.
12332When another "operation" is given, it specifies what should be done with
12333the file. A typical operation is "print".
12334
12335startfile returns as soon as the associated application is launched.
12336There is no option to wait for the application to close, and no way
12337to retrieve the application's exit status.
12338
12339The filepath is relative to the current directory. If you want to use
12340an absolute path, make sure the first character is not a slash ("/");
12341the underlying Win32 ShellExecute function doesn't work if it is.
12342[clinic start generated code]*/
12343
12344static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020012345os_startfile_impl(PyObject *module, path_t *filepath,
12346 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030012347/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070012348{
12349 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080012350
12351 if(!check_ShellExecute()) {
12352 /* If the OS doesn't have ShellExecute, return a
12353 NotImplementedError. */
12354 return PyErr_Format(PyExc_NotImplementedError,
12355 "startfile not available on this platform");
12356 }
12357
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012358 if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -080012359 return NULL;
12360 }
12361
Victor Stinner8c62be82010-05-06 00:08:46 +000012362 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070012363 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080012364 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000012365 Py_END_ALLOW_THREADS
12366
Victor Stinner8c62be82010-05-06 00:08:46 +000012367 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070012368 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020012369 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000012370 }
Steve Dowercc16be82016-09-08 10:35:16 -070012371 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000012372}
Larry Hastings2f936352014-08-05 14:04:04 +100012373#endif /* MS_WINDOWS */
12374
Fred Drake5ab8eaf1999-12-09 21:13:07 +000012375
Martin v. Löwis438b5342002-12-27 10:16:42 +000012376#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100012377/*[clinic input]
12378os.getloadavg
12379
12380Return average recent system load information.
12381
12382Return the number of processes in the system run queue averaged over
12383the last 1, 5, and 15 minutes as a tuple of three floats.
12384Raises OSError if the load average was unobtainable.
12385[clinic start generated code]*/
12386
Larry Hastings2f936352014-08-05 14:04:04 +100012387static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012388os_getloadavg_impl(PyObject *module)
12389/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000012390{
12391 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000012392 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000012393 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
12394 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000012395 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000012396 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000012397}
Larry Hastings2f936352014-08-05 14:04:04 +100012398#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000012399
Larry Hastings2f936352014-08-05 14:04:04 +100012400
12401/*[clinic input]
12402os.device_encoding
12403 fd: int
12404
12405Return a string describing the encoding of a terminal's file descriptor.
12406
12407The file descriptor must be attached to a terminal.
12408If the device is not a terminal, return None.
12409[clinic start generated code]*/
12410
Larry Hastings2f936352014-08-05 14:04:04 +100012411static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012412os_device_encoding_impl(PyObject *module, int fd)
12413/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012414{
Brett Cannonefb00c02012-02-29 18:31:31 -050012415 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000012416}
12417
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012418
Larry Hastings2f936352014-08-05 14:04:04 +100012419#ifdef HAVE_SETRESUID
12420/*[clinic input]
12421os.setresuid
12422
12423 ruid: uid_t
12424 euid: uid_t
12425 suid: uid_t
12426 /
12427
12428Set the current process's real, effective, and saved user ids.
12429[clinic start generated code]*/
12430
Larry Hastings2f936352014-08-05 14:04:04 +100012431static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012432os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
12433/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012434{
Victor Stinner8c62be82010-05-06 00:08:46 +000012435 if (setresuid(ruid, euid, suid) < 0)
12436 return posix_error();
12437 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012438}
Larry Hastings2f936352014-08-05 14:04:04 +100012439#endif /* HAVE_SETRESUID */
12440
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012441
12442#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012443/*[clinic input]
12444os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012445
Larry Hastings2f936352014-08-05 14:04:04 +100012446 rgid: gid_t
12447 egid: gid_t
12448 sgid: gid_t
12449 /
12450
12451Set the current process's real, effective, and saved group ids.
12452[clinic start generated code]*/
12453
Larry Hastings2f936352014-08-05 14:04:04 +100012454static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012455os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
12456/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012457{
Victor Stinner8c62be82010-05-06 00:08:46 +000012458 if (setresgid(rgid, egid, sgid) < 0)
12459 return posix_error();
12460 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012461}
Larry Hastings2f936352014-08-05 14:04:04 +100012462#endif /* HAVE_SETRESGID */
12463
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012464
12465#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100012466/*[clinic input]
12467os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012468
Larry Hastings2f936352014-08-05 14:04:04 +100012469Return a tuple of the current process's real, effective, and saved user ids.
12470[clinic start generated code]*/
12471
Larry Hastings2f936352014-08-05 14:04:04 +100012472static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012473os_getresuid_impl(PyObject *module)
12474/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012475{
Victor Stinner8c62be82010-05-06 00:08:46 +000012476 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012477 if (getresuid(&ruid, &euid, &suid) < 0)
12478 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012479 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
12480 _PyLong_FromUid(euid),
12481 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012482}
Larry Hastings2f936352014-08-05 14:04:04 +100012483#endif /* HAVE_GETRESUID */
12484
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012485
12486#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012487/*[clinic input]
12488os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012489
Larry Hastings2f936352014-08-05 14:04:04 +100012490Return a tuple of the current process's real, effective, and saved group ids.
12491[clinic start generated code]*/
12492
Larry Hastings2f936352014-08-05 14:04:04 +100012493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012494os_getresgid_impl(PyObject *module)
12495/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012496{
12497 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012498 if (getresgid(&rgid, &egid, &sgid) < 0)
12499 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012500 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
12501 _PyLong_FromGid(egid),
12502 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012503}
Larry Hastings2f936352014-08-05 14:04:04 +100012504#endif /* HAVE_GETRESGID */
12505
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012506
Benjamin Peterson9428d532011-09-14 11:45:52 -040012507#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100012508/*[clinic input]
12509os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040012510
Larry Hastings2f936352014-08-05 14:04:04 +100012511 path: path_t(allow_fd=True)
12512 attribute: path_t
12513 *
12514 follow_symlinks: bool = True
12515
12516Return the value of extended attribute attribute on path.
12517
BNMetricsb9427072018-11-02 15:20:19 +000012518path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012519If follow_symlinks is False, and the last element of the path is a symbolic
12520 link, getxattr will examine the symbolic link itself instead of the file
12521 the link points to.
12522
12523[clinic start generated code]*/
12524
Larry Hastings2f936352014-08-05 14:04:04 +100012525static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012526os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012527 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012528/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012529{
12530 Py_ssize_t i;
12531 PyObject *buffer = NULL;
12532
12533 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
12534 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012535
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012536 if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
12537 return NULL;
12538 }
12539
Larry Hastings9cf065c2012-06-22 16:30:09 -070012540 for (i = 0; ; i++) {
12541 void *ptr;
12542 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012543 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070012544 Py_ssize_t buffer_size = buffer_sizes[i];
12545 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100012546 path_error(path);
12547 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012548 }
12549 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
12550 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100012551 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012552 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012553
Larry Hastings9cf065c2012-06-22 16:30:09 -070012554 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012555 if (path->fd >= 0)
12556 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012557 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012558 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012559 else
Larry Hastings2f936352014-08-05 14:04:04 +100012560 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012561 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012562
Larry Hastings9cf065c2012-06-22 16:30:09 -070012563 if (result < 0) {
12564 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012565 if (errno == ERANGE)
12566 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100012567 path_error(path);
12568 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012569 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012570
Larry Hastings9cf065c2012-06-22 16:30:09 -070012571 if (result != buffer_size) {
12572 /* Can only shrink. */
12573 _PyBytes_Resize(&buffer, result);
12574 }
12575 break;
12576 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012577
Larry Hastings9cf065c2012-06-22 16:30:09 -070012578 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012579}
12580
Larry Hastings2f936352014-08-05 14:04:04 +100012581
12582/*[clinic input]
12583os.setxattr
12584
12585 path: path_t(allow_fd=True)
12586 attribute: path_t
12587 value: Py_buffer
12588 flags: int = 0
12589 *
12590 follow_symlinks: bool = True
12591
12592Set extended attribute attribute on path to value.
12593
BNMetricsb9427072018-11-02 15:20:19 +000012594path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012595If follow_symlinks is False, and the last element of the path is a symbolic
12596 link, setxattr will modify the symbolic link itself instead of the file
12597 the link points to.
12598
12599[clinic start generated code]*/
12600
Benjamin Peterson799bd802011-08-31 22:15:17 -040012601static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012602os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012603 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012604/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040012605{
Larry Hastings2f936352014-08-05 14:04:04 +100012606 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012607
Larry Hastings2f936352014-08-05 14:04:04 +100012608 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040012609 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012610
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012611 if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
12612 value->buf, value->len, flags) < 0) {
12613 return NULL;
12614 }
12615
Benjamin Peterson799bd802011-08-31 22:15:17 -040012616 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012617 if (path->fd > -1)
12618 result = fsetxattr(path->fd, attribute->narrow,
12619 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012620 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012621 result = setxattr(path->narrow, attribute->narrow,
12622 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012623 else
Larry Hastings2f936352014-08-05 14:04:04 +100012624 result = lsetxattr(path->narrow, attribute->narrow,
12625 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012626 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012627
Larry Hastings9cf065c2012-06-22 16:30:09 -070012628 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100012629 path_error(path);
12630 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012631 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012632
Larry Hastings2f936352014-08-05 14:04:04 +100012633 Py_RETURN_NONE;
12634}
12635
12636
12637/*[clinic input]
12638os.removexattr
12639
12640 path: path_t(allow_fd=True)
12641 attribute: path_t
12642 *
12643 follow_symlinks: bool = True
12644
12645Remove extended attribute attribute on path.
12646
BNMetricsb9427072018-11-02 15:20:19 +000012647path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012648If follow_symlinks is False, and the last element of the path is a symbolic
12649 link, removexattr will modify the symbolic link itself instead of the file
12650 the link points to.
12651
12652[clinic start generated code]*/
12653
Larry Hastings2f936352014-08-05 14:04:04 +100012654static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012655os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012656 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012657/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012658{
12659 ssize_t result;
12660
12661 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12662 return NULL;
12663
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012664 if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
12665 return NULL;
12666 }
12667
Larry Hastings2f936352014-08-05 14:04:04 +100012668 Py_BEGIN_ALLOW_THREADS;
12669 if (path->fd > -1)
12670 result = fremovexattr(path->fd, attribute->narrow);
12671 else if (follow_symlinks)
12672 result = removexattr(path->narrow, attribute->narrow);
12673 else
12674 result = lremovexattr(path->narrow, attribute->narrow);
12675 Py_END_ALLOW_THREADS;
12676
12677 if (result) {
12678 return path_error(path);
12679 }
12680
12681 Py_RETURN_NONE;
12682}
12683
12684
12685/*[clinic input]
12686os.listxattr
12687
12688 path: path_t(allow_fd=True, nullable=True) = None
12689 *
12690 follow_symlinks: bool = True
12691
12692Return a list of extended attributes on path.
12693
BNMetricsb9427072018-11-02 15:20:19 +000012694path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012695if path is None, listxattr will examine the current directory.
12696If follow_symlinks is False, and the last element of the path is a symbolic
12697 link, listxattr will examine the symbolic link itself instead of the file
12698 the link points to.
12699[clinic start generated code]*/
12700
Larry Hastings2f936352014-08-05 14:04:04 +100012701static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012702os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012703/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012704{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012705 Py_ssize_t i;
12706 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012707 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012708 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012709
Larry Hastings2f936352014-08-05 14:04:04 +100012710 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012711 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012712
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012713 if (PySys_Audit("os.listxattr", "(O)",
12714 path->object ? path->object : Py_None) < 0) {
12715 return NULL;
12716 }
12717
Larry Hastings2f936352014-08-05 14:04:04 +100012718 name = path->narrow ? path->narrow : ".";
12719
Larry Hastings9cf065c2012-06-22 16:30:09 -070012720 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012721 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012722 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012723 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012724 Py_ssize_t buffer_size = buffer_sizes[i];
12725 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012726 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012727 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012728 break;
12729 }
12730 buffer = PyMem_MALLOC(buffer_size);
12731 if (!buffer) {
12732 PyErr_NoMemory();
12733 break;
12734 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012735
Larry Hastings9cf065c2012-06-22 16:30:09 -070012736 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012737 if (path->fd > -1)
12738 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012739 else if (follow_symlinks)
12740 length = listxattr(name, buffer, buffer_size);
12741 else
12742 length = llistxattr(name, buffer, buffer_size);
12743 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012744
Larry Hastings9cf065c2012-06-22 16:30:09 -070012745 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012746 if (errno == ERANGE) {
12747 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012748 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012749 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012750 }
Larry Hastings2f936352014-08-05 14:04:04 +100012751 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012752 break;
12753 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012754
Larry Hastings9cf065c2012-06-22 16:30:09 -070012755 result = PyList_New(0);
12756 if (!result) {
12757 goto exit;
12758 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012759
Larry Hastings9cf065c2012-06-22 16:30:09 -070012760 end = buffer + length;
12761 for (trace = start = buffer; trace != end; trace++) {
12762 if (!*trace) {
12763 int error;
12764 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12765 trace - start);
12766 if (!attribute) {
12767 Py_DECREF(result);
12768 result = NULL;
12769 goto exit;
12770 }
12771 error = PyList_Append(result, attribute);
12772 Py_DECREF(attribute);
12773 if (error) {
12774 Py_DECREF(result);
12775 result = NULL;
12776 goto exit;
12777 }
12778 start = trace + 1;
12779 }
12780 }
12781 break;
12782 }
12783exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012784 if (buffer)
12785 PyMem_FREE(buffer);
12786 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012787}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012788#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012789
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012790
Larry Hastings2f936352014-08-05 14:04:04 +100012791/*[clinic input]
12792os.urandom
12793
12794 size: Py_ssize_t
12795 /
12796
12797Return a bytes object containing random bytes suitable for cryptographic use.
12798[clinic start generated code]*/
12799
Larry Hastings2f936352014-08-05 14:04:04 +100012800static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012801os_urandom_impl(PyObject *module, Py_ssize_t size)
12802/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012803{
12804 PyObject *bytes;
12805 int result;
12806
Georg Brandl2fb477c2012-02-21 00:33:36 +010012807 if (size < 0)
12808 return PyErr_Format(PyExc_ValueError,
12809 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012810 bytes = PyBytes_FromStringAndSize(NULL, size);
12811 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012812 return NULL;
12813
Victor Stinnere66987e2016-09-06 16:33:52 -070012814 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012815 if (result == -1) {
12816 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012817 return NULL;
12818 }
Larry Hastings2f936352014-08-05 14:04:04 +100012819 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012820}
12821
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012822#ifdef HAVE_MEMFD_CREATE
12823/*[clinic input]
12824os.memfd_create
12825
12826 name: FSConverter
12827 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12828
12829[clinic start generated code]*/
12830
12831static PyObject *
12832os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12833/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12834{
12835 int fd;
12836 const char *bytes = PyBytes_AS_STRING(name);
12837 Py_BEGIN_ALLOW_THREADS
12838 fd = memfd_create(bytes, flags);
12839 Py_END_ALLOW_THREADS
12840 if (fd == -1) {
12841 return PyErr_SetFromErrno(PyExc_OSError);
12842 }
12843 return PyLong_FromLong(fd);
12844}
12845#endif
12846
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012847/* Terminal size querying */
12848
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012849PyDoc_STRVAR(TerminalSize_docstring,
12850 "A tuple of (columns, lines) for holding terminal window size");
12851
12852static PyStructSequence_Field TerminalSize_fields[] = {
12853 {"columns", "width of the terminal window in characters"},
12854 {"lines", "height of the terminal window in characters"},
12855 {NULL, NULL}
12856};
12857
12858static PyStructSequence_Desc TerminalSize_desc = {
12859 "os.terminal_size",
12860 TerminalSize_docstring,
12861 TerminalSize_fields,
12862 2,
12863};
12864
12865#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012866/*[clinic input]
12867os.get_terminal_size
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012868
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012869 fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1
12870 /
12871
12872Return the size of the terminal window as (columns, lines).
12873
12874The optional argument fd (default standard output) specifies
12875which file descriptor should be queried.
12876
12877If the file descriptor is not connected to a terminal, an OSError
12878is thrown.
12879
12880This function will only be defined if an implementation is
12881available for this system.
12882
12883shutil.get_terminal_size is the high-level function which should
12884normally be used, os.get_terminal_size is the low-level implementation.
12885[clinic start generated code]*/
12886
12887static PyObject *
12888os_get_terminal_size_impl(PyObject *module, int fd)
12889/*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012890{
12891 int columns, lines;
12892 PyObject *termsize;
12893
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012894 /* Under some conditions stdout may not be connected and
12895 * fileno(stdout) may point to an invalid file descriptor. For example
12896 * GUI apps don't have valid standard streams by default.
12897 *
12898 * If this happens, and the optional fd argument is not present,
12899 * the ioctl below will fail returning EBADF. This is what we want.
12900 */
12901
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012902#ifdef TERMSIZE_USE_IOCTL
12903 {
12904 struct winsize w;
12905 if (ioctl(fd, TIOCGWINSZ, &w))
12906 return PyErr_SetFromErrno(PyExc_OSError);
12907 columns = w.ws_col;
12908 lines = w.ws_row;
12909 }
12910#endif /* TERMSIZE_USE_IOCTL */
12911
12912#ifdef TERMSIZE_USE_CONIO
12913 {
12914 DWORD nhandle;
12915 HANDLE handle;
12916 CONSOLE_SCREEN_BUFFER_INFO csbi;
12917 switch (fd) {
12918 case 0: nhandle = STD_INPUT_HANDLE;
12919 break;
12920 case 1: nhandle = STD_OUTPUT_HANDLE;
12921 break;
12922 case 2: nhandle = STD_ERROR_HANDLE;
12923 break;
12924 default:
12925 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12926 }
12927 handle = GetStdHandle(nhandle);
12928 if (handle == NULL)
12929 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12930 if (handle == INVALID_HANDLE_VALUE)
12931 return PyErr_SetFromWindowsErr(0);
12932
12933 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12934 return PyErr_SetFromWindowsErr(0);
12935
12936 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12937 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12938 }
12939#endif /* TERMSIZE_USE_CONIO */
12940
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012941 PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080012942 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012943 if (termsize == NULL)
12944 return NULL;
12945 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12946 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12947 if (PyErr_Occurred()) {
12948 Py_DECREF(termsize);
12949 return NULL;
12950 }
12951 return termsize;
12952}
12953#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12954
Larry Hastings2f936352014-08-05 14:04:04 +100012955
12956/*[clinic input]
12957os.cpu_count
12958
Charles-François Natali80d62e62015-08-13 20:37:08 +010012959Return the number of CPUs in the system; return None if indeterminable.
12960
12961This number is not equivalent to the number of CPUs the current process can
12962use. The number of usable CPUs can be obtained with
12963``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012964[clinic start generated code]*/
12965
Larry Hastings2f936352014-08-05 14:04:04 +100012966static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012967os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012968/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012969{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012970 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012971#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012972 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012973#elif defined(__hpux)
12974 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12975#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12976 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
pxinwr3405e052020-08-07 13:21:52 +080012977#elif defined(__VXWORKS__)
12978 ncpu = _Py_popcount32(vxCpuEnabledGet());
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012979#elif defined(__DragonFly__) || \
12980 defined(__OpenBSD__) || \
12981 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012982 defined(__NetBSD__) || \
12983 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012984 int mib[2];
12985 size_t len = sizeof(ncpu);
12986 mib[0] = CTL_HW;
12987 mib[1] = HW_NCPU;
12988 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12989 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012990#endif
12991 if (ncpu >= 1)
12992 return PyLong_FromLong(ncpu);
12993 else
12994 Py_RETURN_NONE;
12995}
12996
Victor Stinnerdaf45552013-08-28 00:53:59 +020012997
Larry Hastings2f936352014-08-05 14:04:04 +100012998/*[clinic input]
12999os.get_inheritable -> bool
13000
13001 fd: int
13002 /
13003
13004Get the close-on-exe flag of the specified file descriptor.
13005[clinic start generated code]*/
13006
Larry Hastings2f936352014-08-05 14:04:04 +100013007static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030013008os_get_inheritable_impl(PyObject *module, int fd)
13009/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013010{
Steve Dower8fc89802015-04-12 00:26:27 -040013011 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040013012 _Py_BEGIN_SUPPRESS_IPH
13013 return_value = _Py_get_inheritable(fd);
13014 _Py_END_SUPPRESS_IPH
13015 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100013016}
13017
13018
13019/*[clinic input]
13020os.set_inheritable
13021 fd: int
13022 inheritable: int
13023 /
13024
13025Set the inheritable flag of the specified file descriptor.
13026[clinic start generated code]*/
13027
Larry Hastings2f936352014-08-05 14:04:04 +100013028static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030013029os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
13030/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020013031{
Steve Dower8fc89802015-04-12 00:26:27 -040013032 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013033
Steve Dower8fc89802015-04-12 00:26:27 -040013034 _Py_BEGIN_SUPPRESS_IPH
13035 result = _Py_set_inheritable(fd, inheritable, NULL);
13036 _Py_END_SUPPRESS_IPH
13037 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020013038 return NULL;
13039 Py_RETURN_NONE;
13040}
13041
13042
13043#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100013044/*[clinic input]
13045os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070013046 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100013047 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020013048
Larry Hastings2f936352014-08-05 14:04:04 +100013049Get the close-on-exe flag of the specified file descriptor.
13050[clinic start generated code]*/
13051
Larry Hastings2f936352014-08-05 14:04:04 +100013052static int
Benjamin Petersonca470632016-09-06 13:47:26 -070013053os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070013054/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013055{
13056 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013057
13058 if (!GetHandleInformation((HANDLE)handle, &flags)) {
13059 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100013060 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013061 }
13062
Larry Hastings2f936352014-08-05 14:04:04 +100013063 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013064}
13065
Victor Stinnerdaf45552013-08-28 00:53:59 +020013066
Larry Hastings2f936352014-08-05 14:04:04 +100013067/*[clinic input]
13068os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070013069 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100013070 inheritable: bool
13071 /
13072
13073Set the inheritable flag of the specified handle.
13074[clinic start generated code]*/
13075
Larry Hastings2f936352014-08-05 14:04:04 +100013076static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070013077os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040013078 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070013079/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100013080{
13081 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020013082 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
13083 PyErr_SetFromWindowsErr(0);
13084 return NULL;
13085 }
13086 Py_RETURN_NONE;
13087}
Larry Hastings2f936352014-08-05 14:04:04 +100013088#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010013089
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013090#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013091/*[clinic input]
13092os.get_blocking -> bool
13093 fd: int
13094 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013095
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013096Get the blocking mode of the file descriptor.
13097
13098Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
13099[clinic start generated code]*/
13100
13101static int
13102os_get_blocking_impl(PyObject *module, int fd)
13103/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013104{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013105 int blocking;
13106
Steve Dower8fc89802015-04-12 00:26:27 -040013107 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013108 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040013109 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013110 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013111}
13112
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013113/*[clinic input]
13114os.set_blocking
13115 fd: int
13116 blocking: bool(accept={int})
13117 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013118
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013119Set the blocking mode of the specified file descriptor.
13120
13121Set the O_NONBLOCK flag if blocking is False,
13122clear the O_NONBLOCK flag otherwise.
13123[clinic start generated code]*/
13124
13125static PyObject *
13126os_set_blocking_impl(PyObject *module, int fd, int blocking)
13127/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013128{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013129 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013130
Steve Dower8fc89802015-04-12 00:26:27 -040013131 _Py_BEGIN_SUPPRESS_IPH
13132 result = _Py_set_blocking(fd, blocking);
13133 _Py_END_SUPPRESS_IPH
13134 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020013135 return NULL;
13136 Py_RETURN_NONE;
13137}
13138#endif /* !MS_WINDOWS */
13139
13140
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013141/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080013142class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013143[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080013144/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013145
13146typedef struct {
13147 PyObject_HEAD
13148 PyObject *name;
13149 PyObject *path;
13150 PyObject *stat;
13151 PyObject *lstat;
13152#ifdef MS_WINDOWS
13153 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010013154 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010013155 int got_file_index;
13156#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010013157#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013158 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013159#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013160 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013161 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010013162#endif
13163} DirEntry;
13164
Eddie Elizondob3966632019-11-05 07:16:14 -080013165static PyObject *
13166_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
13167{
13168 PyErr_Format(PyExc_TypeError,
13169 "cannot create '%.100s' instances", _PyType_Name(type));
13170 return NULL;
13171}
13172
Victor Stinner6036e442015-03-08 01:58:04 +010013173static void
13174DirEntry_dealloc(DirEntry *entry)
13175{
Eddie Elizondob3966632019-11-05 07:16:14 -080013176 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010013177 Py_XDECREF(entry->name);
13178 Py_XDECREF(entry->path);
13179 Py_XDECREF(entry->stat);
13180 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080013181 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13182 free_func(entry);
13183 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013184}
13185
13186/* Forward reference */
13187static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013188DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
13189 int follow_symlinks, unsigned short mode_bits);
Victor Stinner6036e442015-03-08 01:58:04 +010013190
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013191/*[clinic input]
13192os.DirEntry.is_symlink -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013193 defining_class: defining_class
13194 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013195
13196Return True if the entry is a symbolic link; cached per entry.
13197[clinic start generated code]*/
13198
Victor Stinner6036e442015-03-08 01:58:04 +010013199static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013200os_DirEntry_is_symlink_impl(DirEntry *self, PyTypeObject *defining_class)
13201/*[clinic end generated code: output=293096d589b6d47c input=e9acc5ee4d511113]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013202{
13203#ifdef MS_WINDOWS
13204 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010013205#elif defined(HAVE_DIRENT_D_TYPE)
13206 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013207 if (self->d_type != DT_UNKNOWN)
13208 return self->d_type == DT_LNK;
13209 else
Victor Stinner97f33c32020-05-14 18:05:58 +020013210 return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010013211#else
13212 /* POSIX without d_type */
Victor Stinner97f33c32020-05-14 18:05:58 +020013213 return DirEntry_test_mode(defining_class, self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010013214#endif
13215}
13216
13217static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013218DirEntry_fetch_stat(PyObject *module, DirEntry *self, int follow_symlinks)
Victor Stinner6036e442015-03-08 01:58:04 +010013219{
13220 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013221 STRUCT_STAT st;
13222 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010013223
13224#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013225 if (!PyUnicode_FSDecoder(self->path, &ub))
13226 return NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013227#if USE_UNICODE_WCHAR_CACHE
13228_Py_COMP_DIAG_PUSH
13229_Py_COMP_DIAG_IGNORE_DEPR_DECLS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013230 const wchar_t *path = PyUnicode_AsUnicode(ub);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013231_Py_COMP_DIAG_POP
13232#else /* USE_UNICODE_WCHAR_CACHE */
13233 wchar_t *path = PyUnicode_AsWideCharString(ub, NULL);
13234 Py_DECREF(ub);
13235#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013236#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013237 if (!PyUnicode_FSConverter(self->path, &ub))
13238 return NULL;
13239 const char *path = PyBytes_AS_STRING(ub);
13240 if (self->dir_fd != DEFAULT_DIR_FD) {
13241#ifdef HAVE_FSTATAT
Ronald Oussoren41761932020-11-08 10:05:27 +010013242 if (HAVE_FSTATAT_RUNTIME) {
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013243 result = fstatat(self->dir_fd, path, &st,
13244 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
Ronald Oussoren41761932020-11-08 10:05:27 +010013245 } else
13246
13247#endif /* HAVE_FSTATAT */
13248 {
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013249 Py_DECREF(ub);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013250 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
13251 return NULL;
Ronald Oussoren41761932020-11-08 10:05:27 +010013252 }
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013253 }
13254 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013255#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013256 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013257 if (follow_symlinks)
13258 result = STAT(path, &st);
13259 else
13260 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013261 }
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013262#if defined(MS_WINDOWS) && !USE_UNICODE_WCHAR_CACHE
13263 PyMem_Free(path);
13264#else /* USE_UNICODE_WCHAR_CACHE */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013265 Py_DECREF(ub);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013266#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013267
13268 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013269 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013270
Victor Stinner97f33c32020-05-14 18:05:58 +020013271 return _pystat_fromstructstat(module, &st);
Victor Stinner6036e442015-03-08 01:58:04 +010013272}
13273
13274static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013275DirEntry_get_lstat(PyTypeObject *defining_class, DirEntry *self)
Victor Stinner6036e442015-03-08 01:58:04 +010013276{
13277 if (!self->lstat) {
Victor Stinner97f33c32020-05-14 18:05:58 +020013278 PyObject *module = PyType_GetModule(defining_class);
Victor Stinner6036e442015-03-08 01:58:04 +010013279#ifdef MS_WINDOWS
Victor Stinner97f33c32020-05-14 18:05:58 +020013280 self->lstat = _pystat_fromstructstat(module, &self->win32_lstat);
Victor Stinner6036e442015-03-08 01:58:04 +010013281#else /* POSIX */
Victor Stinner97f33c32020-05-14 18:05:58 +020013282 self->lstat = DirEntry_fetch_stat(module, self, 0);
Victor Stinner6036e442015-03-08 01:58:04 +010013283#endif
13284 }
13285 Py_XINCREF(self->lstat);
13286 return self->lstat;
13287}
13288
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013289/*[clinic input]
13290os.DirEntry.stat
Victor Stinner97f33c32020-05-14 18:05:58 +020013291 defining_class: defining_class
13292 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013293 *
13294 follow_symlinks: bool = True
13295
13296Return stat_result object for the entry; cached per entry.
13297[clinic start generated code]*/
13298
Victor Stinner6036e442015-03-08 01:58:04 +010013299static PyObject *
Victor Stinner97f33c32020-05-14 18:05:58 +020013300os_DirEntry_stat_impl(DirEntry *self, PyTypeObject *defining_class,
13301 int follow_symlinks)
13302/*[clinic end generated code: output=23f803e19c3e780e input=e816273c4e67ee98]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013303{
Victor Stinner97f33c32020-05-14 18:05:58 +020013304 if (!follow_symlinks) {
13305 return DirEntry_get_lstat(defining_class, self);
13306 }
Victor Stinner6036e442015-03-08 01:58:04 +010013307
13308 if (!self->stat) {
Victor Stinner97f33c32020-05-14 18:05:58 +020013309 int result = os_DirEntry_is_symlink_impl(self, defining_class);
13310 if (result == -1) {
Victor Stinner6036e442015-03-08 01:58:04 +010013311 return NULL;
Victor Stinner97f33c32020-05-14 18:05:58 +020013312 }
13313 if (result) {
13314 PyObject *module = PyType_GetModule(defining_class);
13315 self->stat = DirEntry_fetch_stat(module, self, 1);
13316 }
13317 else {
13318 self->stat = DirEntry_get_lstat(defining_class, self);
13319 }
Victor Stinner6036e442015-03-08 01:58:04 +010013320 }
13321
13322 Py_XINCREF(self->stat);
13323 return self->stat;
13324}
13325
Victor Stinner6036e442015-03-08 01:58:04 +010013326/* Set exception and return -1 on error, 0 for False, 1 for True */
13327static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013328DirEntry_test_mode(PyTypeObject *defining_class, DirEntry *self,
13329 int follow_symlinks, unsigned short mode_bits)
Victor Stinner6036e442015-03-08 01:58:04 +010013330{
13331 PyObject *stat = NULL;
13332 PyObject *st_mode = NULL;
13333 long mode;
13334 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010013335#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013336 int is_symlink;
13337 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010013338#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013339#ifdef MS_WINDOWS
13340 unsigned long dir_bits;
13341#endif
13342
13343#ifdef MS_WINDOWS
13344 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
13345 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010013346#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013347 is_symlink = self->d_type == DT_LNK;
13348 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
13349#endif
13350
Victor Stinner35a97c02015-03-08 02:59:09 +010013351#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013352 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010013353#endif
Victor Stinner97f33c32020-05-14 18:05:58 +020013354 stat = os_DirEntry_stat_impl(self, defining_class, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010013355 if (!stat) {
13356 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
13357 /* If file doesn't exist (anymore), then return False
13358 (i.e., say it's not a file/directory) */
13359 PyErr_Clear();
13360 return 0;
13361 }
13362 goto error;
13363 }
Victor Stinner97f33c32020-05-14 18:05:58 +020013364 _posixstate* state = get_posix_state(PyType_GetModule(defining_class));
13365 st_mode = PyObject_GetAttr(stat, state->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010013366 if (!st_mode)
13367 goto error;
13368
13369 mode = PyLong_AsLong(st_mode);
13370 if (mode == -1 && PyErr_Occurred())
13371 goto error;
13372 Py_CLEAR(st_mode);
13373 Py_CLEAR(stat);
13374 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010013375#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010013376 }
13377 else if (is_symlink) {
13378 assert(mode_bits != S_IFLNK);
13379 result = 0;
13380 }
13381 else {
13382 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
13383#ifdef MS_WINDOWS
13384 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
13385 if (mode_bits == S_IFDIR)
13386 result = dir_bits != 0;
13387 else
13388 result = dir_bits == 0;
13389#else /* POSIX */
13390 if (mode_bits == S_IFDIR)
13391 result = self->d_type == DT_DIR;
13392 else
13393 result = self->d_type == DT_REG;
13394#endif
13395 }
Victor Stinner35a97c02015-03-08 02:59:09 +010013396#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013397
13398 return result;
13399
13400error:
13401 Py_XDECREF(st_mode);
13402 Py_XDECREF(stat);
13403 return -1;
13404}
13405
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013406/*[clinic input]
13407os.DirEntry.is_dir -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013408 defining_class: defining_class
13409 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013410 *
13411 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010013412
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013413Return True if the entry is a directory; cached per entry.
13414[clinic start generated code]*/
13415
13416static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013417os_DirEntry_is_dir_impl(DirEntry *self, PyTypeObject *defining_class,
13418 int follow_symlinks)
13419/*[clinic end generated code: output=0cd453b9c0987fdf input=1a4ffd6dec9920cb]*/
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013420{
Victor Stinner97f33c32020-05-14 18:05:58 +020013421 return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010013422}
13423
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013424/*[clinic input]
13425os.DirEntry.is_file -> bool
Victor Stinner97f33c32020-05-14 18:05:58 +020013426 defining_class: defining_class
13427 /
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013428 *
13429 follow_symlinks: bool = True
13430
13431Return True if the entry is a file; cached per entry.
13432[clinic start generated code]*/
13433
13434static int
Victor Stinner97f33c32020-05-14 18:05:58 +020013435os_DirEntry_is_file_impl(DirEntry *self, PyTypeObject *defining_class,
13436 int follow_symlinks)
13437/*[clinic end generated code: output=f7c277ab5ba80908 input=0a64c5a12e802e3b]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013438{
Victor Stinner97f33c32020-05-14 18:05:58 +020013439 return DirEntry_test_mode(defining_class, self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010013440}
13441
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013442/*[clinic input]
13443os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010013444
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013445Return inode of the entry; cached per entry.
13446[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013447
13448static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013449os_DirEntry_inode_impl(DirEntry *self)
13450/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013451{
13452#ifdef MS_WINDOWS
13453 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013454 PyObject *unicode;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013455 STRUCT_STAT stat;
13456 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010013457
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013458 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010013459 return NULL;
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013460#if USE_UNICODE_WCHAR_CACHE
13461_Py_COMP_DIAG_PUSH
13462_Py_COMP_DIAG_IGNORE_DEPR_DECLS
13463 const wchar_t *path = PyUnicode_AsUnicode(unicode);
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013464 result = LSTAT(path, &stat);
13465 Py_DECREF(unicode);
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030013466_Py_COMP_DIAG_POP
13467#else /* USE_UNICODE_WCHAR_CACHE */
13468 wchar_t *path = PyUnicode_AsWideCharString(unicode, NULL);
13469 Py_DECREF(unicode);
13470 result = LSTAT(path, &stat);
13471 PyMem_Free(path);
13472#endif /* USE_UNICODE_WCHAR_CACHE */
Victor Stinner6036e442015-03-08 01:58:04 +010013473
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030013474 if (result != 0)
13475 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013476
13477 self->win32_file_index = stat.st_ino;
13478 self->got_file_index = 1;
13479 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010013480 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
13481 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010013482#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020013483 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
13484 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010013485#endif
13486}
13487
13488static PyObject *
13489DirEntry_repr(DirEntry *self)
13490{
13491 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
13492}
13493
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013494/*[clinic input]
13495os.DirEntry.__fspath__
13496
13497Returns the path for the entry.
13498[clinic start generated code]*/
13499
Brett Cannon96881cd2016-06-10 14:37:21 -070013500static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013501os_DirEntry___fspath___impl(DirEntry *self)
13502/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070013503{
13504 Py_INCREF(self->path);
13505 return self->path;
13506}
13507
Victor Stinner6036e442015-03-08 01:58:04 +010013508static PyMemberDef DirEntry_members[] = {
13509 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
13510 "the entry's base filename, relative to scandir() \"path\" argument"},
13511 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
13512 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
13513 {NULL}
13514};
13515
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013516#include "clinic/posixmodule.c.h"
13517
Victor Stinner6036e442015-03-08 01:58:04 +010013518static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013519 OS_DIRENTRY_IS_DIR_METHODDEF
13520 OS_DIRENTRY_IS_FILE_METHODDEF
13521 OS_DIRENTRY_IS_SYMLINK_METHODDEF
13522 OS_DIRENTRY_STAT_METHODDEF
13523 OS_DIRENTRY_INODE_METHODDEF
13524 OS_DIRENTRY___FSPATH___METHODDEF
Batuhan Taşkayaf9dd51e2020-04-08 00:37:19 +030013525 {"__class_getitem__", (PyCFunction)Py_GenericAlias,
13526 METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Victor Stinner6036e442015-03-08 01:58:04 +010013527 {NULL}
13528};
13529
Eddie Elizondob3966632019-11-05 07:16:14 -080013530static PyType_Slot DirEntryType_slots[] = {
13531 {Py_tp_new, _disabled_new},
13532 {Py_tp_dealloc, DirEntry_dealloc},
13533 {Py_tp_repr, DirEntry_repr},
13534 {Py_tp_methods, DirEntry_methods},
13535 {Py_tp_members, DirEntry_members},
13536 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010013537};
13538
Eddie Elizondob3966632019-11-05 07:16:14 -080013539static PyType_Spec DirEntryType_spec = {
13540 MODNAME ".DirEntry",
13541 sizeof(DirEntry),
13542 0,
13543 Py_TPFLAGS_DEFAULT,
13544 DirEntryType_slots
13545};
13546
13547
Victor Stinner6036e442015-03-08 01:58:04 +010013548#ifdef MS_WINDOWS
13549
13550static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030013551join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010013552{
13553 Py_ssize_t path_len;
13554 Py_ssize_t size;
13555 wchar_t *result;
13556 wchar_t ch;
13557
13558 if (!path_wide) { /* Default arg: "." */
13559 path_wide = L".";
13560 path_len = 1;
13561 }
13562 else {
13563 path_len = wcslen(path_wide);
13564 }
13565
13566 /* The +1's are for the path separator and the NUL */
13567 size = path_len + 1 + wcslen(filename) + 1;
13568 result = PyMem_New(wchar_t, size);
13569 if (!result) {
13570 PyErr_NoMemory();
13571 return NULL;
13572 }
13573 wcscpy(result, path_wide);
13574 if (path_len > 0) {
13575 ch = result[path_len - 1];
13576 if (ch != SEP && ch != ALTSEP && ch != L':')
13577 result[path_len++] = SEP;
13578 wcscpy(result + path_len, filename);
13579 }
13580 return result;
13581}
13582
13583static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +020013584DirEntry_from_find_data(PyObject *module, path_t *path, WIN32_FIND_DATAW *dataW)
Victor Stinner6036e442015-03-08 01:58:04 +010013585{
13586 DirEntry *entry;
13587 BY_HANDLE_FILE_INFORMATION file_info;
13588 ULONG reparse_tag;
13589 wchar_t *joined_path;
13590
Victor Stinner1c2fa782020-05-10 11:05:29 +020013591 PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013592 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013593 if (!entry)
13594 return NULL;
13595 entry->name = NULL;
13596 entry->path = NULL;
13597 entry->stat = NULL;
13598 entry->lstat = NULL;
13599 entry->got_file_index = 0;
13600
13601 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
13602 if (!entry->name)
13603 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013604 if (path->narrow) {
13605 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
13606 if (!entry->name)
13607 goto error;
13608 }
Victor Stinner6036e442015-03-08 01:58:04 +010013609
13610 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
13611 if (!joined_path)
13612 goto error;
13613
13614 entry->path = PyUnicode_FromWideChar(joined_path, -1);
13615 PyMem_Free(joined_path);
13616 if (!entry->path)
13617 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013618 if (path->narrow) {
13619 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
13620 if (!entry->path)
13621 goto error;
13622 }
Victor Stinner6036e442015-03-08 01:58:04 +010013623
Steve Dowercc16be82016-09-08 10:35:16 -070013624 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010013625 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
13626
13627 return (PyObject *)entry;
13628
13629error:
13630 Py_DECREF(entry);
13631 return NULL;
13632}
13633
13634#else /* POSIX */
13635
13636static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013637join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010013638{
13639 Py_ssize_t path_len;
13640 Py_ssize_t size;
13641 char *result;
13642
13643 if (!path_narrow) { /* Default arg: "." */
13644 path_narrow = ".";
13645 path_len = 1;
13646 }
13647 else {
13648 path_len = strlen(path_narrow);
13649 }
13650
13651 if (filename_len == -1)
13652 filename_len = strlen(filename);
13653
13654 /* The +1's are for the path separator and the NUL */
13655 size = path_len + 1 + filename_len + 1;
13656 result = PyMem_New(char, size);
13657 if (!result) {
13658 PyErr_NoMemory();
13659 return NULL;
13660 }
13661 strcpy(result, path_narrow);
13662 if (path_len > 0 && result[path_len - 1] != '/')
13663 result[path_len++] = '/';
13664 strcpy(result + path_len, filename);
13665 return result;
13666}
13667
13668static PyObject *
Victor Stinner1c2fa782020-05-10 11:05:29 +020013669DirEntry_from_posix_info(PyObject *module, path_t *path, const char *name,
13670 Py_ssize_t name_len, ino_t d_ino
Victor Stinner35a97c02015-03-08 02:59:09 +010013671#ifdef HAVE_DIRENT_D_TYPE
13672 , unsigned char d_type
13673#endif
13674 )
Victor Stinner6036e442015-03-08 01:58:04 +010013675{
13676 DirEntry *entry;
13677 char *joined_path;
13678
Victor Stinner1c2fa782020-05-10 11:05:29 +020013679 PyObject *DirEntryType = get_posix_state(module)->DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013680 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013681 if (!entry)
13682 return NULL;
13683 entry->name = NULL;
13684 entry->path = NULL;
13685 entry->stat = NULL;
13686 entry->lstat = NULL;
13687
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013688 if (path->fd != -1) {
13689 entry->dir_fd = path->fd;
13690 joined_path = NULL;
13691 }
13692 else {
13693 entry->dir_fd = DEFAULT_DIR_FD;
13694 joined_path = join_path_filename(path->narrow, name, name_len);
13695 if (!joined_path)
13696 goto error;
13697 }
Victor Stinner6036e442015-03-08 01:58:04 +010013698
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030013699 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010013700 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013701 if (joined_path)
13702 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013703 }
13704 else {
13705 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013706 if (joined_path)
13707 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013708 }
13709 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013710 if (!entry->name)
13711 goto error;
13712
13713 if (path->fd != -1) {
13714 entry->path = entry->name;
13715 Py_INCREF(entry->path);
13716 }
13717 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010013718 goto error;
13719
Victor Stinner35a97c02015-03-08 02:59:09 +010013720#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013721 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013722#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013723 entry->d_ino = d_ino;
13724
13725 return (PyObject *)entry;
13726
13727error:
13728 Py_XDECREF(entry);
13729 return NULL;
13730}
13731
13732#endif
13733
13734
13735typedef struct {
13736 PyObject_HEAD
13737 path_t path;
13738#ifdef MS_WINDOWS
13739 HANDLE handle;
13740 WIN32_FIND_DATAW file_data;
13741 int first_time;
13742#else /* POSIX */
13743 DIR *dirp;
13744#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013745#ifdef HAVE_FDOPENDIR
13746 int fd;
13747#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013748} ScandirIterator;
13749
13750#ifdef MS_WINDOWS
13751
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013752static int
13753ScandirIterator_is_closed(ScandirIterator *iterator)
13754{
13755 return iterator->handle == INVALID_HANDLE_VALUE;
13756}
13757
Victor Stinner6036e442015-03-08 01:58:04 +010013758static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013759ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013760{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013761 HANDLE handle = iterator->handle;
13762
13763 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013764 return;
13765
Victor Stinner6036e442015-03-08 01:58:04 +010013766 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013767 Py_BEGIN_ALLOW_THREADS
13768 FindClose(handle);
13769 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013770}
13771
13772static PyObject *
13773ScandirIterator_iternext(ScandirIterator *iterator)
13774{
13775 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13776 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013777 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013778
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013779 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013780 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013781 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013782
13783 while (1) {
13784 if (!iterator->first_time) {
13785 Py_BEGIN_ALLOW_THREADS
13786 success = FindNextFileW(iterator->handle, file_data);
13787 Py_END_ALLOW_THREADS
13788 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013789 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013790 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013791 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013792 break;
13793 }
13794 }
13795 iterator->first_time = 0;
13796
13797 /* Skip over . and .. */
13798 if (wcscmp(file_data->cFileName, L".") != 0 &&
Victor Stinner1c2fa782020-05-10 11:05:29 +020013799 wcscmp(file_data->cFileName, L"..") != 0)
13800 {
13801 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
13802 entry = DirEntry_from_find_data(module, &iterator->path, file_data);
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013803 if (!entry)
13804 break;
13805 return entry;
13806 }
Victor Stinner6036e442015-03-08 01:58:04 +010013807
13808 /* Loop till we get a non-dot directory or finish iterating */
13809 }
13810
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013811 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013812 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013813 return NULL;
13814}
13815
13816#else /* POSIX */
13817
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013818static int
13819ScandirIterator_is_closed(ScandirIterator *iterator)
13820{
13821 return !iterator->dirp;
13822}
13823
Victor Stinner6036e442015-03-08 01:58:04 +010013824static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013825ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013826{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013827 DIR *dirp = iterator->dirp;
13828
13829 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013830 return;
13831
Victor Stinner6036e442015-03-08 01:58:04 +010013832 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013833 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013834#ifdef HAVE_FDOPENDIR
13835 if (iterator->path.fd != -1)
13836 rewinddir(dirp);
13837#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013838 closedir(dirp);
13839 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013840 return;
13841}
13842
13843static PyObject *
13844ScandirIterator_iternext(ScandirIterator *iterator)
13845{
13846 struct dirent *direntp;
13847 Py_ssize_t name_len;
13848 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013849 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013850
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013851 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013852 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013853 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013854
13855 while (1) {
13856 errno = 0;
13857 Py_BEGIN_ALLOW_THREADS
13858 direntp = readdir(iterator->dirp);
13859 Py_END_ALLOW_THREADS
13860
13861 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013862 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013863 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013864 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013865 break;
13866 }
13867
13868 /* Skip over . and .. */
13869 name_len = NAMLEN(direntp);
13870 is_dot = direntp->d_name[0] == '.' &&
13871 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13872 if (!is_dot) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020013873 PyObject *module = PyType_GetModule(Py_TYPE(iterator));
13874 entry = DirEntry_from_posix_info(module,
13875 &iterator->path, direntp->d_name,
13876 name_len, direntp->d_ino
Victor Stinner35a97c02015-03-08 02:59:09 +010013877#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner1c2fa782020-05-10 11:05:29 +020013878 , direntp->d_type
Victor Stinner35a97c02015-03-08 02:59:09 +010013879#endif
13880 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013881 if (!entry)
13882 break;
13883 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013884 }
13885
13886 /* Loop till we get a non-dot directory or finish iterating */
13887 }
13888
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013889 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013890 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013891 return NULL;
13892}
13893
13894#endif
13895
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013896static PyObject *
13897ScandirIterator_close(ScandirIterator *self, PyObject *args)
13898{
13899 ScandirIterator_closedir(self);
13900 Py_RETURN_NONE;
13901}
13902
13903static PyObject *
13904ScandirIterator_enter(PyObject *self, PyObject *args)
13905{
13906 Py_INCREF(self);
13907 return self;
13908}
13909
13910static PyObject *
13911ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13912{
13913 ScandirIterator_closedir(self);
13914 Py_RETURN_NONE;
13915}
13916
Victor Stinner6036e442015-03-08 01:58:04 +010013917static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013918ScandirIterator_finalize(ScandirIterator *iterator)
13919{
13920 PyObject *error_type, *error_value, *error_traceback;
13921
13922 /* Save the current exception, if any. */
13923 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13924
13925 if (!ScandirIterator_is_closed(iterator)) {
13926 ScandirIterator_closedir(iterator);
13927
13928 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13929 "unclosed scandir iterator %R", iterator)) {
13930 /* Spurious errors can appear at shutdown */
13931 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13932 PyErr_WriteUnraisable((PyObject *) iterator);
13933 }
13934 }
13935 }
13936
Victor Stinner7bfa4092016-03-23 00:43:54 +010013937 path_cleanup(&iterator->path);
13938
13939 /* Restore the saved exception. */
13940 PyErr_Restore(error_type, error_value, error_traceback);
13941}
13942
13943static void
Victor Stinner6036e442015-03-08 01:58:04 +010013944ScandirIterator_dealloc(ScandirIterator *iterator)
13945{
Eddie Elizondob3966632019-11-05 07:16:14 -080013946 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010013947 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13948 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013949
Eddie Elizondob3966632019-11-05 07:16:14 -080013950 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13951 free_func(iterator);
13952 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013953}
13954
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013955static PyMethodDef ScandirIterator_methods[] = {
13956 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13957 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13958 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13959 {NULL}
13960};
13961
Eddie Elizondob3966632019-11-05 07:16:14 -080013962static PyType_Slot ScandirIteratorType_slots[] = {
13963 {Py_tp_new, _disabled_new},
13964 {Py_tp_dealloc, ScandirIterator_dealloc},
13965 {Py_tp_finalize, ScandirIterator_finalize},
13966 {Py_tp_iter, PyObject_SelfIter},
13967 {Py_tp_iternext, ScandirIterator_iternext},
13968 {Py_tp_methods, ScandirIterator_methods},
13969 {0, 0},
13970};
13971
13972static PyType_Spec ScandirIteratorType_spec = {
13973 MODNAME ".ScandirIterator",
13974 sizeof(ScandirIterator),
13975 0,
Victor Stinner97f33c32020-05-14 18:05:58 +020013976 // bpo-40549: Py_TPFLAGS_BASETYPE should not be used, since
13977 // PyType_GetModule(Py_TYPE(self)) doesn't work on a subclass instance.
Eddie Elizondob3966632019-11-05 07:16:14 -080013978 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
13979 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010013980};
13981
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013982/*[clinic input]
13983os.scandir
13984
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013985 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013986
13987Return an iterator of DirEntry objects for given path.
13988
BNMetricsb9427072018-11-02 15:20:19 +000013989path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013990is bytes, the names of yielded DirEntry objects will also be bytes; in
13991all other circumstances they will be str.
13992
13993If path is None, uses the path='.'.
13994[clinic start generated code]*/
13995
Victor Stinner6036e442015-03-08 01:58:04 +010013996static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013997os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013998/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013999{
14000 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010014001#ifdef MS_WINDOWS
14002 wchar_t *path_strW;
14003#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014004 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014005#ifdef HAVE_FDOPENDIR
14006 int fd = -1;
14007#endif
Victor Stinner6036e442015-03-08 01:58:04 +010014008#endif
14009
Steve Dower60419a72019-06-24 08:42:54 -070014010 if (PySys_Audit("os.scandir", "O",
14011 path->object ? path->object : Py_None) < 0) {
14012 return NULL;
14013 }
14014
Hai Shif707d942020-03-16 21:15:01 +080014015 PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014016 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010014017 if (!iterator)
14018 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010014019
14020#ifdef MS_WINDOWS
14021 iterator->handle = INVALID_HANDLE_VALUE;
14022#else
14023 iterator->dirp = NULL;
14024#endif
14025
Serhiy Storchaka095ef732017-02-09 20:05:51 +020014026 /* Move the ownership to iterator->path */
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014027 memcpy(&iterator->path, path, sizeof(path_t));
14028 memset(path, 0, sizeof(path_t));
Victor Stinner6036e442015-03-08 01:58:04 +010014029
14030#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010014031 iterator->first_time = 1;
14032
14033 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
14034 if (!path_strW)
14035 goto error;
14036
14037 Py_BEGIN_ALLOW_THREADS
14038 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
14039 Py_END_ALLOW_THREADS
14040
14041 PyMem_Free(path_strW);
14042
14043 if (iterator->handle == INVALID_HANDLE_VALUE) {
14044 path_error(&iterator->path);
14045 goto error;
14046 }
14047#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010014048 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014049#ifdef HAVE_FDOPENDIR
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014050 if (iterator->path.fd != -1) {
Ronald Oussoren41761932020-11-08 10:05:27 +010014051 if (HAVE_FDOPENDIR_RUNTIME) {
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014052 /* closedir() closes the FD, so we duplicate it */
Serhiy Storchaka4c8f09d2020-07-10 23:26:06 +030014053 fd = _Py_dup(iterator->path.fd);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014054 if (fd == -1)
14055 goto error;
14056
14057 Py_BEGIN_ALLOW_THREADS
14058 iterator->dirp = fdopendir(fd);
14059 Py_END_ALLOW_THREADS
Ronald Oussoren41761932020-11-08 10:05:27 +010014060 } else {
14061 PyErr_SetString(PyExc_TypeError,
14062 "scandir: path should be string, bytes, os.PathLike or None, not int");
14063 return NULL;
14064 }
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014065 }
14066 else
14067#endif
14068 {
14069 if (iterator->path.narrow)
14070 path_str = iterator->path.narrow;
14071 else
14072 path_str = ".";
14073
14074 Py_BEGIN_ALLOW_THREADS
14075 iterator->dirp = opendir(path_str);
14076 Py_END_ALLOW_THREADS
14077 }
Victor Stinner6036e442015-03-08 01:58:04 +010014078
14079 if (!iterator->dirp) {
14080 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030014081#ifdef HAVE_FDOPENDIR
14082 if (fd != -1) {
14083 Py_BEGIN_ALLOW_THREADS
14084 close(fd);
14085 Py_END_ALLOW_THREADS
14086 }
14087#endif
Victor Stinner6036e442015-03-08 01:58:04 +010014088 goto error;
14089 }
14090#endif
14091
14092 return (PyObject *)iterator;
14093
14094error:
14095 Py_DECREF(iterator);
14096 return NULL;
14097}
14098
Ethan Furman410ef8e2016-06-04 12:06:26 -070014099/*
14100 Return the file system path representation of the object.
14101
14102 If the object is str or bytes, then allow it to pass through with
14103 an incremented refcount. If the object defines __fspath__(), then
14104 return the result of that method. All other types raise a TypeError.
14105*/
14106PyObject *
14107PyOS_FSPath(PyObject *path)
14108{
Brett Cannon3f9183b2016-08-26 14:44:48 -070014109 /* For error message reasons, this function is manually inlined in
14110 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070014111 PyObject *func = NULL;
14112 PyObject *path_repr = NULL;
14113
14114 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
14115 Py_INCREF(path);
14116 return path;
14117 }
14118
14119 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
14120 if (NULL == func) {
14121 return PyErr_Format(PyExc_TypeError,
14122 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014123 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080014124 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070014125 }
14126
Victor Stinnerf17c3de2016-12-06 18:46:19 +010014127 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070014128 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070014129 if (NULL == path_repr) {
14130 return NULL;
14131 }
14132
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014133 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
14134 PyErr_Format(PyExc_TypeError,
14135 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080014136 "not %.200s", _PyType_Name(Py_TYPE(path)),
14137 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070014138 Py_DECREF(path_repr);
14139 return NULL;
14140 }
14141
Ethan Furman410ef8e2016-06-04 12:06:26 -070014142 return path_repr;
14143}
14144
14145/*[clinic input]
14146os.fspath
14147
14148 path: object
14149
14150Return the file system path representation of the object.
14151
Brett Cannonb4f43e92016-06-09 14:32:08 -070014152If the object is str or bytes, then allow it to pass through as-is. If the
14153object defines __fspath__(), then return the result of that method. All other
14154types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070014155[clinic start generated code]*/
14156
14157static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030014158os_fspath_impl(PyObject *module, PyObject *path)
14159/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070014160{
14161 return PyOS_FSPath(path);
14162}
Victor Stinner6036e442015-03-08 01:58:04 +010014163
Victor Stinner9b1f4742016-09-06 16:18:52 -070014164#ifdef HAVE_GETRANDOM_SYSCALL
14165/*[clinic input]
14166os.getrandom
14167
14168 size: Py_ssize_t
14169 flags: int=0
14170
14171Obtain a series of random bytes.
14172[clinic start generated code]*/
14173
14174static PyObject *
14175os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
14176/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
14177{
Victor Stinner9b1f4742016-09-06 16:18:52 -070014178 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020014179 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014180
14181 if (size < 0) {
14182 errno = EINVAL;
14183 return posix_error();
14184 }
14185
Victor Stinnerec2319c2016-09-20 23:00:59 +020014186 bytes = PyBytes_FromStringAndSize(NULL, size);
14187 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070014188 PyErr_NoMemory();
14189 return NULL;
14190 }
14191
14192 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020014193 n = syscall(SYS_getrandom,
14194 PyBytes_AS_STRING(bytes),
14195 PyBytes_GET_SIZE(bytes),
14196 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070014197 if (n < 0 && errno == EINTR) {
14198 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020014199 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014200 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020014201
14202 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070014203 continue;
14204 }
14205 break;
14206 }
14207
14208 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070014209 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020014210 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014211 }
14212
Victor Stinnerec2319c2016-09-20 23:00:59 +020014213 if (n != size) {
14214 _PyBytes_Resize(&bytes, n);
14215 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070014216
14217 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020014218
14219error:
14220 Py_DECREF(bytes);
14221 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070014222}
14223#endif /* HAVE_GETRANDOM_SYSCALL */
14224
Steve Dower2438cdf2019-03-29 16:37:16 -070014225#ifdef MS_WINDOWS
14226/* bpo-36085: Helper functions for managing DLL search directories
14227 * on win32
14228 */
14229
14230typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
14231typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
14232
14233/*[clinic input]
14234os._add_dll_directory
14235
14236 path: path_t
14237
14238Add a path to the DLL search path.
14239
14240This search path is used when resolving dependencies for imported
14241extension modules (the module itself is resolved through sys.path),
14242and also by ctypes.
14243
14244Returns an opaque value that may be passed to os.remove_dll_directory
14245to remove this directory from the search path.
14246[clinic start generated code]*/
14247
14248static PyObject *
14249os__add_dll_directory_impl(PyObject *module, path_t *path)
14250/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
14251{
14252 HMODULE hKernel32;
14253 PAddDllDirectory AddDllDirectory;
14254 DLL_DIRECTORY_COOKIE cookie = 0;
14255 DWORD err = 0;
14256
Saiyang Gou7514f4f2020-02-12 23:47:42 -080014257 if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
14258 return NULL;
14259 }
14260
Steve Dower2438cdf2019-03-29 16:37:16 -070014261 /* For Windows 7, we have to load this. As this will be a fairly
14262 infrequent operation, just do it each time. Kernel32 is always
14263 loaded. */
14264 Py_BEGIN_ALLOW_THREADS
14265 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
14266 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
14267 hKernel32, "AddDllDirectory")) ||
14268 !(cookie = (*AddDllDirectory)(path->wide))) {
14269 err = GetLastError();
14270 }
14271 Py_END_ALLOW_THREADS
14272
14273 if (err) {
14274 return win32_error_object_err("add_dll_directory",
14275 path->object, err);
14276 }
14277
14278 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
14279}
14280
14281/*[clinic input]
14282os._remove_dll_directory
14283
14284 cookie: object
14285
14286Removes a path from the DLL search path.
14287
14288The parameter is an opaque value that was returned from
14289os.add_dll_directory. You can only remove directories that you added
14290yourself.
14291[clinic start generated code]*/
14292
14293static PyObject *
14294os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
14295/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
14296{
14297 HMODULE hKernel32;
14298 PRemoveDllDirectory RemoveDllDirectory;
14299 DLL_DIRECTORY_COOKIE cookieValue;
14300 DWORD err = 0;
14301
14302 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
14303 PyErr_SetString(PyExc_TypeError,
14304 "Provided cookie was not returned from os.add_dll_directory");
14305 return NULL;
14306 }
14307
14308 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
14309 cookie, "DLL directory cookie");
14310
14311 /* For Windows 7, we have to load this. As this will be a fairly
14312 infrequent operation, just do it each time. Kernel32 is always
14313 loaded. */
14314 Py_BEGIN_ALLOW_THREADS
14315 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
14316 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
14317 hKernel32, "RemoveDllDirectory")) ||
14318 !(*RemoveDllDirectory)(cookieValue)) {
14319 err = GetLastError();
14320 }
14321 Py_END_ALLOW_THREADS
14322
14323 if (err) {
14324 return win32_error_object_err("remove_dll_directory",
14325 NULL, err);
14326 }
14327
14328 if (PyCapsule_SetName(cookie, NULL)) {
14329 return NULL;
14330 }
14331
14332 Py_RETURN_NONE;
14333}
14334
14335#endif
Larry Hastings31826802013-10-19 00:09:25 -070014336
Victor Stinner65a796e2020-04-01 18:49:29 +020014337
14338/* Only check if WIFEXITED is available: expect that it comes
14339 with WEXITSTATUS, WIFSIGNALED, etc.
14340
14341 os.waitstatus_to_exitcode() is implemented in C and not in Python, so
14342 subprocess can safely call it during late Python finalization without
14343 risking that used os attributes were set to None by _PyImport_Cleanup(). */
14344#if defined(WIFEXITED) || defined(MS_WINDOWS)
14345/*[clinic input]
14346os.waitstatus_to_exitcode
14347
Victor Stinner9bee32b2020-04-22 16:30:35 +020014348 status as status_obj: object
Victor Stinner65a796e2020-04-01 18:49:29 +020014349
14350Convert a wait status to an exit code.
14351
14352On Unix:
14353
14354* If WIFEXITED(status) is true, return WEXITSTATUS(status).
14355* If WIFSIGNALED(status) is true, return -WTERMSIG(status).
14356* Otherwise, raise a ValueError.
14357
14358On Windows, return status shifted right by 8 bits.
14359
14360On Unix, if the process is being traced or if waitpid() was called with
14361WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
14362This function must not be called if WIFSTOPPED(status) is true.
14363[clinic start generated code]*/
14364
14365static PyObject *
Victor Stinner9bee32b2020-04-22 16:30:35 +020014366os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
14367/*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/
Victor Stinner65a796e2020-04-01 18:49:29 +020014368{
14369#ifndef MS_WINDOWS
Victor Stinner9bee32b2020-04-22 16:30:35 +020014370 int status = _PyLong_AsInt(status_obj);
14371 if (status == -1 && PyErr_Occurred()) {
14372 return NULL;
14373 }
14374
Victor Stinner65a796e2020-04-01 18:49:29 +020014375 WAIT_TYPE wait_status;
14376 WAIT_STATUS_INT(wait_status) = status;
14377 int exitcode;
14378 if (WIFEXITED(wait_status)) {
14379 exitcode = WEXITSTATUS(wait_status);
14380 /* Sanity check to provide warranty on the function behavior.
14381 It should not occur in practice */
14382 if (exitcode < 0) {
14383 PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
14384 return NULL;
14385 }
14386 }
14387 else if (WIFSIGNALED(wait_status)) {
14388 int signum = WTERMSIG(wait_status);
14389 /* Sanity check to provide warranty on the function behavior.
14390 It should not occurs in practice */
14391 if (signum <= 0) {
14392 PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
14393 return NULL;
14394 }
14395 exitcode = -signum;
14396 } else if (WIFSTOPPED(wait_status)) {
14397 /* Status only received if the process is being traced
14398 or if waitpid() was called with WUNTRACED option. */
14399 int signum = WSTOPSIG(wait_status);
14400 PyErr_Format(PyExc_ValueError,
14401 "process stopped by delivery of signal %i",
14402 signum);
14403 return NULL;
14404 }
14405 else {
14406 PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
14407 return NULL;
14408 }
14409 return PyLong_FromLong(exitcode);
14410#else
14411 /* Windows implementation: see os.waitpid() implementation
14412 which uses _cwait(). */
Victor Stinner9bee32b2020-04-22 16:30:35 +020014413 unsigned long long status = PyLong_AsUnsignedLongLong(status_obj);
14414 if (status == (unsigned long long)-1 && PyErr_Occurred()) {
14415 return NULL;
14416 }
14417
14418 unsigned long long exitcode = (status >> 8);
14419 /* ExitProcess() accepts an UINT type:
14420 reject exit code which doesn't fit in an UINT */
14421 if (exitcode > UINT_MAX) {
14422 PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode);
14423 return NULL;
14424 }
14425 return PyLong_FromUnsignedLong((unsigned long)exitcode);
Victor Stinner65a796e2020-04-01 18:49:29 +020014426#endif
14427}
14428#endif
14429
14430
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014431static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070014432
14433 OS_STAT_METHODDEF
14434 OS_ACCESS_METHODDEF
14435 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014436 OS_CHDIR_METHODDEF
14437 OS_CHFLAGS_METHODDEF
14438 OS_CHMOD_METHODDEF
14439 OS_FCHMOD_METHODDEF
14440 OS_LCHMOD_METHODDEF
14441 OS_CHOWN_METHODDEF
14442 OS_FCHOWN_METHODDEF
14443 OS_LCHOWN_METHODDEF
14444 OS_LCHFLAGS_METHODDEF
14445 OS_CHROOT_METHODDEF
14446 OS_CTERMID_METHODDEF
14447 OS_GETCWD_METHODDEF
14448 OS_GETCWDB_METHODDEF
14449 OS_LINK_METHODDEF
14450 OS_LISTDIR_METHODDEF
14451 OS_LSTAT_METHODDEF
14452 OS_MKDIR_METHODDEF
14453 OS_NICE_METHODDEF
14454 OS_GETPRIORITY_METHODDEF
14455 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014456 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030014457 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014458 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010014459 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014460 OS_RENAME_METHODDEF
14461 OS_REPLACE_METHODDEF
14462 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014463 OS_SYMLINK_METHODDEF
14464 OS_SYSTEM_METHODDEF
14465 OS_UMASK_METHODDEF
14466 OS_UNAME_METHODDEF
14467 OS_UNLINK_METHODDEF
14468 OS_REMOVE_METHODDEF
14469 OS_UTIME_METHODDEF
14470 OS_TIMES_METHODDEF
14471 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014472 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014473 OS_EXECV_METHODDEF
14474 OS_EXECVE_METHODDEF
14475 OS_SPAWNV_METHODDEF
14476 OS_SPAWNVE_METHODDEF
14477 OS_FORK1_METHODDEF
14478 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020014479 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014480 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
14481 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
14482 OS_SCHED_GETPARAM_METHODDEF
14483 OS_SCHED_GETSCHEDULER_METHODDEF
14484 OS_SCHED_RR_GET_INTERVAL_METHODDEF
14485 OS_SCHED_SETPARAM_METHODDEF
14486 OS_SCHED_SETSCHEDULER_METHODDEF
14487 OS_SCHED_YIELD_METHODDEF
14488 OS_SCHED_SETAFFINITY_METHODDEF
14489 OS_SCHED_GETAFFINITY_METHODDEF
14490 OS_OPENPTY_METHODDEF
14491 OS_FORKPTY_METHODDEF
14492 OS_GETEGID_METHODDEF
14493 OS_GETEUID_METHODDEF
14494 OS_GETGID_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014495 OS_GETGROUPLIST_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014496 OS_GETGROUPS_METHODDEF
14497 OS_GETPID_METHODDEF
14498 OS_GETPGRP_METHODDEF
14499 OS_GETPPID_METHODDEF
14500 OS_GETUID_METHODDEF
14501 OS_GETLOGIN_METHODDEF
14502 OS_KILL_METHODDEF
14503 OS_KILLPG_METHODDEF
14504 OS_PLOCK_METHODDEF
Steve Dowercc16be82016-09-08 10:35:16 -070014505 OS_STARTFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014506 OS_SETUID_METHODDEF
14507 OS_SETEUID_METHODDEF
14508 OS_SETREUID_METHODDEF
14509 OS_SETGID_METHODDEF
14510 OS_SETEGID_METHODDEF
14511 OS_SETREGID_METHODDEF
14512 OS_SETGROUPS_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014513 OS_INITGROUPS_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014514 OS_GETPGID_METHODDEF
14515 OS_SETPGRP_METHODDEF
14516 OS_WAIT_METHODDEF
14517 OS_WAIT3_METHODDEF
14518 OS_WAIT4_METHODDEF
14519 OS_WAITID_METHODDEF
14520 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080014521 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014522 OS_GETSID_METHODDEF
14523 OS_SETSID_METHODDEF
14524 OS_SETPGID_METHODDEF
14525 OS_TCGETPGRP_METHODDEF
14526 OS_TCSETPGRP_METHODDEF
14527 OS_OPEN_METHODDEF
14528 OS_CLOSE_METHODDEF
14529 OS_CLOSERANGE_METHODDEF
14530 OS_DEVICE_ENCODING_METHODDEF
14531 OS_DUP_METHODDEF
14532 OS_DUP2_METHODDEF
14533 OS_LOCKF_METHODDEF
14534 OS_LSEEK_METHODDEF
14535 OS_READ_METHODDEF
14536 OS_READV_METHODDEF
14537 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014538 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014539 OS_WRITE_METHODDEF
14540 OS_WRITEV_METHODDEF
14541 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014542 OS_PWRITEV_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014543 OS_SENDFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014544 OS_FSTAT_METHODDEF
14545 OS_ISATTY_METHODDEF
14546 OS_PIPE_METHODDEF
14547 OS_PIPE2_METHODDEF
14548 OS_MKFIFO_METHODDEF
14549 OS_MKNOD_METHODDEF
14550 OS_MAJOR_METHODDEF
14551 OS_MINOR_METHODDEF
14552 OS_MAKEDEV_METHODDEF
14553 OS_FTRUNCATE_METHODDEF
14554 OS_TRUNCATE_METHODDEF
14555 OS_POSIX_FALLOCATE_METHODDEF
14556 OS_POSIX_FADVISE_METHODDEF
14557 OS_PUTENV_METHODDEF
14558 OS_UNSETENV_METHODDEF
14559 OS_STRERROR_METHODDEF
14560 OS_FCHDIR_METHODDEF
14561 OS_FSYNC_METHODDEF
14562 OS_SYNC_METHODDEF
14563 OS_FDATASYNC_METHODDEF
14564 OS_WCOREDUMP_METHODDEF
14565 OS_WIFCONTINUED_METHODDEF
14566 OS_WIFSTOPPED_METHODDEF
14567 OS_WIFSIGNALED_METHODDEF
14568 OS_WIFEXITED_METHODDEF
14569 OS_WEXITSTATUS_METHODDEF
14570 OS_WTERMSIG_METHODDEF
14571 OS_WSTOPSIG_METHODDEF
14572 OS_FSTATVFS_METHODDEF
14573 OS_STATVFS_METHODDEF
14574 OS_CONFSTR_METHODDEF
14575 OS_SYSCONF_METHODDEF
14576 OS_FPATHCONF_METHODDEF
14577 OS_PATHCONF_METHODDEF
14578 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030014579 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014580 OS__GETDISKUSAGE_METHODDEF
14581 OS__GETFINALPATHNAME_METHODDEF
14582 OS__GETVOLUMEPATHNAME_METHODDEF
14583 OS_GETLOADAVG_METHODDEF
14584 OS_URANDOM_METHODDEF
14585 OS_SETRESUID_METHODDEF
14586 OS_SETRESGID_METHODDEF
14587 OS_GETRESUID_METHODDEF
14588 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000014589
Larry Hastings2f936352014-08-05 14:04:04 +100014590 OS_GETXATTR_METHODDEF
14591 OS_SETXATTR_METHODDEF
14592 OS_REMOVEXATTR_METHODDEF
14593 OS_LISTXATTR_METHODDEF
14594
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014595 OS_GET_TERMINAL_SIZE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014596 OS_CPU_COUNT_METHODDEF
14597 OS_GET_INHERITABLE_METHODDEF
14598 OS_SET_INHERITABLE_METHODDEF
14599 OS_GET_HANDLE_INHERITABLE_METHODDEF
14600 OS_SET_HANDLE_INHERITABLE_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014601 OS_GET_BLOCKING_METHODDEF
14602 OS_SET_BLOCKING_METHODDEF
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014603 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070014604 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070014605 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014606 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070014607 OS__ADD_DLL_DIRECTORY_METHODDEF
14608 OS__REMOVE_DLL_DIRECTORY_METHODDEF
Victor Stinner65a796e2020-04-01 18:49:29 +020014609 OS_WAITSTATUS_TO_EXITCODE_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000014610 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014611};
14612
Barry Warsaw4a342091996-12-19 23:50:02 +000014613static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014614all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000014615{
Guido van Rossum94f6f721999-01-06 18:42:14 +000014616#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014617 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014618#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014619#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014620 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014621#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014622#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014623 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014624#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014625#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014626 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014627#endif
Fred Drakec9680921999-12-13 16:37:25 +000014628#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014629 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000014630#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014631#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014632 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014633#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014634#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014635 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014636#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014637#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014638 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014639#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014640#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014641 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014642#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014643#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014644 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014645#endif
14646#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014647 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014648#endif
14649#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014650 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014651#endif
14652#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014653 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014654#endif
14655#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014656 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014657#endif
14658#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014659 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014660#endif
14661#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014662 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014663#endif
14664#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014665 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014666#endif
14667#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014668 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014669#endif
14670#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014671 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014672#endif
14673#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014674 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014675#endif
14676#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014677 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014678#endif
14679#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014680 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014681#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000014682#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014683 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014684#endif
14685#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014686 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014687#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014688#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014689 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014690#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014691#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014692 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014693#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014694#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000014695#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014696 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014697#endif
14698#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014699 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014700#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014701#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014702#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014703 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014704#endif
14705#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014706 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014707#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014708#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014709 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014710#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014711#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014712 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014713#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020014714#ifdef O_TMPFILE
14715 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
14716#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014717#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014718 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014719#endif
14720#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014721 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014722#endif
14723#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014724 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014725#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020014726#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014727 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020014728#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014729#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014730 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014731#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014732
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014733
Jesus Cea94363612012-06-22 18:32:07 +020014734#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014735 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014736#endif
14737#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014738 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014739#endif
14740
Tim Peters5aa91602002-01-30 05:46:57 +000014741/* MS Windows */
14742#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000014743 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014744 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014745#endif
14746#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000014747 /* Optimize for short life (keep in memory). */
14748 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014749 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014750#endif
14751#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000014752 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014753 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014754#endif
14755#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000014756 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014757 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014758#endif
14759#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000014760 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014761 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014762#endif
14763
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014764/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014765#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000014766 /* Send a SIGIO signal whenever input or output
14767 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014768 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014769#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014770#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000014771 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014772 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014773#endif
14774#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000014775 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014776 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014777#endif
14778#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000014779 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014780 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014781#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014782#ifdef O_NOLINKS
14783 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014784 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014785#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014786#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000014787 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014788 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014789#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000014790
Victor Stinner8c62be82010-05-06 00:08:46 +000014791 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014792#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014793 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014794#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014795#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014796 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014797#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014798#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014799 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014800#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014801#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014802 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014803#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014804#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014805 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014806#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014807#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014808 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014809#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014810#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014811 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014812#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014813#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014814 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014815#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014816#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014817 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014818#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014819#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014820 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014821#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014822#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014823 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014824#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014825#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014826 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014827#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014828#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014829 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014830#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014831#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014832 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014833#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014834#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014835 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014836#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014837#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014838 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014839#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014840#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014841 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014842#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014843
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000014844 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014845#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014846 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014847#endif /* ST_RDONLY */
14848#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014849 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014850#endif /* ST_NOSUID */
14851
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014852 /* GNU extensions */
14853#ifdef ST_NODEV
14854 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14855#endif /* ST_NODEV */
14856#ifdef ST_NOEXEC
14857 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14858#endif /* ST_NOEXEC */
14859#ifdef ST_SYNCHRONOUS
14860 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14861#endif /* ST_SYNCHRONOUS */
14862#ifdef ST_MANDLOCK
14863 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14864#endif /* ST_MANDLOCK */
14865#ifdef ST_WRITE
14866 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14867#endif /* ST_WRITE */
14868#ifdef ST_APPEND
14869 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14870#endif /* ST_APPEND */
14871#ifdef ST_NOATIME
14872 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14873#endif /* ST_NOATIME */
14874#ifdef ST_NODIRATIME
14875 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14876#endif /* ST_NODIRATIME */
14877#ifdef ST_RELATIME
14878 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14879#endif /* ST_RELATIME */
14880
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014881 /* FreeBSD sendfile() constants */
14882#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014883 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014884#endif
14885#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014886 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014887#endif
14888#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014889 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014890#endif
14891
Ross Lagerwall7807c352011-03-17 20:20:30 +020014892 /* constants for posix_fadvise */
14893#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014894 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014895#endif
14896#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014897 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014898#endif
14899#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014900 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014901#endif
14902#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014903 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014904#endif
14905#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014906 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014907#endif
14908#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014909 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014910#endif
14911
14912 /* constants for waitid */
14913#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014914 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14915 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14916 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080014917#ifdef P_PIDFD
14918 if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
14919#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014920#endif
14921#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014922 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014923#endif
14924#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014925 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014926#endif
14927#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014928 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014929#endif
14930#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014931 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014932#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014933#ifdef CLD_KILLED
14934 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
14935#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014936#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014937 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014938#endif
14939#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014940 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014941#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014942#ifdef CLD_STOPPED
14943 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
14944#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014945#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014946 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014947#endif
14948
14949 /* constants for lockf */
14950#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014951 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014952#endif
14953#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014954 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014955#endif
14956#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014957 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014958#endif
14959#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014960 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014961#endif
14962
Pablo Galindo4defba32018-01-27 16:16:37 +000014963#ifdef RWF_DSYNC
14964 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14965#endif
14966#ifdef RWF_HIPRI
14967 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14968#endif
14969#ifdef RWF_SYNC
14970 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14971#endif
14972#ifdef RWF_NOWAIT
14973 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14974#endif
YoSTEALTH76ef2552020-05-27 15:32:22 -060014975#ifdef RWF_APPEND
14976 if (PyModule_AddIntConstant(m, "RWF_APPEND", RWF_APPEND)) return -1;
14977#endif
Pablo Galindo4defba32018-01-27 16:16:37 +000014978
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014979/* constants for posix_spawn */
14980#ifdef HAVE_POSIX_SPAWN
14981 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14982 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14983 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14984#endif
14985
pxinwrf2d7ac72019-05-21 18:46:37 +080014986#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014987 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14988 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014989 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014990#endif
14991#ifdef HAVE_SPAWNV
14992 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014993 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014994#endif
14995
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014996#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014997#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014998 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014999#endif
15000#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015001 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015002#endif
15003#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015004 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070015005#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015006#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080015007 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015008#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015009#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015010 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015011#endif
15012#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015013 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015014#endif
15015#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015016 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015017#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015018#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015019 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015020#endif
15021#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015022 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015023#endif
15024#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015025 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015026#endif
15027#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015028 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020015029#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015030#endif
15031
Benjamin Peterson9428d532011-09-14 11:45:52 -040015032#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015033 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
15034 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
15035 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040015036#endif
15037
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015038#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015039 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015040#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015041#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015042 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015043#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015044#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015045 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015046#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015047#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015048 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015049#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015050#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015051 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015052#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015053#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015054 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015055#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030015056#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020015057 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020015058#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010015059#if HAVE_DECL_RTLD_MEMBER
15060 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
15061#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020015062
Victor Stinner9b1f4742016-09-06 16:18:52 -070015063#ifdef HAVE_GETRANDOM_SYSCALL
15064 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
15065 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
15066#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015067#ifdef HAVE_MEMFD_CREATE
15068 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
15069 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
15070#ifdef MFD_HUGETLB
15071 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015072#endif
15073#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015074 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015075#endif
15076#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015077 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015078#endif
15079#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015080 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015081#endif
15082#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015083 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015084#endif
15085#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015086 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015087#endif
15088#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015089 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015090#endif
15091#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015092 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015093#endif
15094#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015095 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015096#endif
15097#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015098 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015099#endif
15100#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015101 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015102#endif
15103#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015104 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015105#endif
15106#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015107 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015108#endif
15109#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015110 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060015111#endif
15112#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015113 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
15114#endif
15115#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070015116
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020015117#if defined(__APPLE__)
15118 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
15119#endif
15120
Steve Dower2438cdf2019-03-29 16:37:16 -070015121#ifdef MS_WINDOWS
15122 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
15123 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
15124 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
15125 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
15126 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
15127#endif
15128
Victor Stinner8c62be82010-05-06 00:08:46 +000015129 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000015130}
15131
15132
Ronald Oussoren41761932020-11-08 10:05:27 +010015133
15134#define PROBE(name, test) \
15135 static int name(void) \
15136 { \
15137 if (test) { \
15138 return 1; \
15139 } else { \
15140 return 0; \
15141 } \
15142 }
15143
15144#ifdef HAVE_FSTATAT
15145PROBE(probe_fstatat, HAVE_FSTATAT_RUNTIME)
15146#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -070015147
15148#ifdef HAVE_FACCESSAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015149PROBE(probe_faccessat, HAVE_FACCESSAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015150#endif
15151
15152#ifdef HAVE_FCHMODAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015153PROBE(probe_fchmodat, HAVE_FCHMODAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015154#endif
15155
Larry Hastings00964ed2013-08-12 13:49:30 -040015156#ifdef HAVE_FCHOWNAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015157PROBE(probe_fchownat, HAVE_FCHOWNAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015158#endif
15159
15160#ifdef HAVE_LINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015161PROBE(probe_linkat, HAVE_LINKAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015162#endif
15163
Ronald Oussoren41761932020-11-08 10:05:27 +010015164#ifdef HAVE_FDOPENDIR
15165PROBE(probe_fdopendir, HAVE_FDOPENDIR_RUNTIME)
Zackery Spytz43fdbd22019-05-29 13:57:07 -060015166#endif
15167
Larry Hastings9cf065c2012-06-22 16:30:09 -070015168#ifdef HAVE_MKDIRAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015169PROBE(probe_mkdirat, HAVE_MKDIRAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015170#endif
15171
15172#ifdef HAVE_RENAMEAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015173PROBE(probe_renameat, HAVE_RENAMEAT_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015174#endif
15175
15176#ifdef HAVE_UNLINKAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015177PROBE(probe_unlinkat, HAVE_UNLINKAT_RUNTIME)
15178#endif
15179
15180#ifdef HAVE_OPENAT
15181PROBE(probe_openat, HAVE_OPENAT_RUNTIME)
15182#endif
15183
15184#ifdef HAVE_READLINKAT
15185PROBE(probe_readlinkat, HAVE_READLINKAT_RUNTIME)
15186#endif
15187
15188#ifdef HAVE_SYMLINKAT
15189PROBE(probe_symlinkat, HAVE_SYMLINKAT_RUNTIME)
15190#endif
15191
15192#ifdef HAVE_FUTIMENS
15193PROBE(probe_futimens, HAVE_FUTIMENS_RUNTIME)
Larry Hastings9cf065c2012-06-22 16:30:09 -070015194#endif
15195
15196#ifdef HAVE_UTIMENSAT
Ronald Oussoren41761932020-11-08 10:05:27 +010015197PROBE(probe_utimensat, HAVE_UTIMENSAT_RUNTIME)
15198#endif
15199
15200
15201
15202
15203static const struct have_function {
15204 const char * const label;
15205 int (*probe)(void);
15206} have_functions[] = {
15207
15208#ifdef HAVE_FACCESSAT
15209 { "HAVE_FACCESSAT", probe_faccessat },
15210#endif
15211
15212#ifdef HAVE_FCHDIR
15213 { "HAVE_FCHDIR", NULL },
15214#endif
15215
15216#ifdef HAVE_FCHMOD
15217 { "HAVE_FCHMOD", NULL },
15218#endif
15219
15220#ifdef HAVE_FCHMODAT
15221 { "HAVE_FCHMODAT", probe_fchmodat },
15222#endif
15223
15224#ifdef HAVE_FCHOWN
15225 { "HAVE_FCHOWN", NULL },
15226#endif
15227
15228#ifdef HAVE_FCHOWNAT
15229 { "HAVE_FCHOWNAT", probe_fchownat },
15230#endif
15231
15232#ifdef HAVE_FEXECVE
15233 { "HAVE_FEXECVE", NULL },
15234#endif
15235
15236#ifdef HAVE_FDOPENDIR
15237 { "HAVE_FDOPENDIR", probe_fdopendir },
15238#endif
15239
15240#ifdef HAVE_FPATHCONF
15241 { "HAVE_FPATHCONF", NULL },
15242#endif
15243
15244#ifdef HAVE_FSTATAT
15245 { "HAVE_FSTATAT", probe_fstatat },
15246#endif
15247
15248#ifdef HAVE_FSTATVFS
15249 { "HAVE_FSTATVFS", NULL },
15250#endif
15251
15252#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
15253 { "HAVE_FTRUNCATE", NULL },
15254#endif
15255
15256#ifdef HAVE_FUTIMENS
15257 { "HAVE_FUTIMENS", probe_futimens },
15258#endif
15259
15260#ifdef HAVE_FUTIMES
15261 { "HAVE_FUTIMES", NULL },
15262#endif
15263
15264#ifdef HAVE_FUTIMESAT
15265 { "HAVE_FUTIMESAT", NULL },
15266#endif
15267
15268#ifdef HAVE_LINKAT
15269 { "HAVE_LINKAT", probe_linkat },
15270#endif
15271
15272#ifdef HAVE_LCHFLAGS
15273 { "HAVE_LCHFLAGS", NULL },
15274#endif
15275
15276#ifdef HAVE_LCHMOD
15277 { "HAVE_LCHMOD", NULL },
15278#endif
15279
15280#ifdef HAVE_LCHOWN
15281 { "HAVE_LCHOWN", NULL },
15282#endif
15283
15284#ifdef HAVE_LSTAT
15285 { "HAVE_LSTAT", NULL },
15286#endif
15287
15288#ifdef HAVE_LUTIMES
15289 { "HAVE_LUTIMES", NULL },
15290#endif
15291
15292#ifdef HAVE_MEMFD_CREATE
15293 { "HAVE_MEMFD_CREATE", NULL },
15294#endif
15295
15296#ifdef HAVE_MKDIRAT
15297 { "HAVE_MKDIRAT", probe_mkdirat },
15298#endif
15299
15300#ifdef HAVE_MKFIFOAT
15301 { "HAVE_MKFIFOAT", NULL },
15302#endif
15303
15304#ifdef HAVE_MKNODAT
15305 { "HAVE_MKNODAT", NULL },
15306#endif
15307
15308#ifdef HAVE_OPENAT
15309 { "HAVE_OPENAT", probe_openat },
15310#endif
15311
15312#ifdef HAVE_READLINKAT
15313 { "HAVE_READLINKAT", probe_readlinkat },
15314#endif
15315
15316#ifdef HAVE_RENAMEAT
15317 { "HAVE_RENAMEAT", probe_renameat },
15318#endif
15319
15320#ifdef HAVE_SYMLINKAT
15321 { "HAVE_SYMLINKAT", probe_symlinkat },
15322#endif
15323
15324#ifdef HAVE_UNLINKAT
15325 { "HAVE_UNLINKAT", probe_unlinkat },
15326#endif
15327
15328#ifdef HAVE_UTIMENSAT
15329 { "HAVE_UTIMENSAT", probe_utimensat },
Larry Hastings9cf065c2012-06-22 16:30:09 -070015330#endif
15331
15332#ifdef MS_WINDOWS
Ronald Oussoren41761932020-11-08 10:05:27 +010015333 { "MS_WINDOWS", NULL },
Larry Hastings9cf065c2012-06-22 16:30:09 -070015334#endif
15335
Ronald Oussoren41761932020-11-08 10:05:27 +010015336 { NULL, NULL }
Larry Hastings9cf065c2012-06-22 16:30:09 -070015337};
15338
15339
Victor Stinner1c2fa782020-05-10 11:05:29 +020015340static int
15341posixmodule_exec(PyObject *m)
Guido van Rossumb6775db1994-08-01 11:34:53 +000015342{
Victor Stinner97f33c32020-05-14 18:05:58 +020015343 _posixstate *state = get_posix_state(m);
Tim Peters5aa91602002-01-30 05:46:57 +000015344
Ronald Oussoren41761932020-11-08 10:05:27 +010015345#if defined(HAVE_PWRITEV)
15346 if (HAVE_PWRITEV_RUNTIME) {} else {
15347 PyObject* dct = PyModule_GetDict(m);
15348
15349 if (dct == NULL) {
15350 return -1;
15351 }
15352
15353 if (PyDict_DelItemString(dct, "pwritev") == -1) {
15354 PyErr_Clear();
15355 }
15356 if (PyDict_DelItemString(dct, "preadv") == -1) {
15357 PyErr_Clear();
15358 }
15359 }
15360#endif
15361
Victor Stinner8c62be82010-05-06 00:08:46 +000015362 /* Initialize environ dictionary */
Victor Stinner97f33c32020-05-14 18:05:58 +020015363 PyObject *v = convertenviron();
Victor Stinner8c62be82010-05-06 00:08:46 +000015364 Py_XINCREF(v);
15365 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015366 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +000015367 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000015368
Victor Stinner8c62be82010-05-06 00:08:46 +000015369 if (all_ins(m))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015370 return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000015371
Victor Stinner8c62be82010-05-06 00:08:46 +000015372 if (setup_confname_tables(m))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015373 return -1;
Fred Drakebec628d1999-12-15 18:31:10 +000015374
Victor Stinner8c62be82010-05-06 00:08:46 +000015375 Py_INCREF(PyExc_OSError);
15376 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000015377
Ross Lagerwall7807c352011-03-17 20:20:30 +020015378#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080015379 waitid_result_desc.name = MODNAME ".waitid_result";
15380 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
15381 if (WaitidResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015382 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015383 }
15384 Py_INCREF(WaitidResultType);
15385 PyModule_AddObject(m, "waitid_result", WaitidResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015386 state->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020015387#endif
15388
Eddie Elizondob3966632019-11-05 07:16:14 -080015389 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
15390 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
15391 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
15392 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
15393 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
15394 if (StatResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015395 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015396 }
15397 Py_INCREF(StatResultType);
15398 PyModule_AddObject(m, "stat_result", StatResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015399 state->StatResultType = StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015400 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
15401 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015402
Eddie Elizondob3966632019-11-05 07:16:14 -080015403 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
15404 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
15405 if (StatVFSResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015406 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015407 }
15408 Py_INCREF(StatVFSResultType);
15409 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015410 state->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015411#ifdef NEED_TICKS_PER_SECOND
15412# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080015413 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015414# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080015415 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015416# else
Eddie Elizondob3966632019-11-05 07:16:14 -080015417 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000015418# endif
15419#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050015420
William Orr81574b82018-10-01 22:19:56 -070015421#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080015422 sched_param_desc.name = MODNAME ".sched_param";
15423 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
15424 if (SchedParamType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015425 return -1;
Victor Stinner8c62be82010-05-06 00:08:46 +000015426 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015427 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080015428 PyModule_AddObject(m, "sched_param", SchedParamType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015429 state->SchedParamType = SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015430 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050015431#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000015432
Eddie Elizondob3966632019-11-05 07:16:14 -080015433 /* initialize TerminalSize_info */
15434 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
15435 if (TerminalSizeType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015436 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015437 }
15438 Py_INCREF(TerminalSizeType);
15439 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015440 state->TerminalSizeType = TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015441
15442 /* initialize scandir types */
Victor Stinner1c2fa782020-05-10 11:05:29 +020015443 PyObject *ScandirIteratorType = PyType_FromModuleAndSpec(m, &ScandirIteratorType_spec, NULL);
Eddie Elizondob3966632019-11-05 07:16:14 -080015444 if (ScandirIteratorType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015445 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015446 }
Victor Stinner97f33c32020-05-14 18:05:58 +020015447 state->ScandirIteratorType = ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015448
Victor Stinner1c2fa782020-05-10 11:05:29 +020015449 PyObject *DirEntryType = PyType_FromModuleAndSpec(m, &DirEntryType_spec, NULL);
Eddie Elizondob3966632019-11-05 07:16:14 -080015450 if (DirEntryType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015451 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015452 }
15453 Py_INCREF(DirEntryType);
15454 PyModule_AddObject(m, "DirEntry", DirEntryType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015455 state->DirEntryType = DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080015456
Larry Hastings605a62d2012-06-24 04:33:36 -070015457 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080015458 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015459 if (TimesResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015460 return -1;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015461 }
Eddie Elizondob3966632019-11-05 07:16:14 -080015462 Py_INCREF(TimesResultType);
15463 PyModule_AddObject(m, "times_result", TimesResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015464 state->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070015465
Eddie Elizondob3966632019-11-05 07:16:14 -080015466 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015467 if (UnameResultType == NULL) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015468 return -1;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015469 }
Eddie Elizondob3966632019-11-05 07:16:14 -080015470 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080015471 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Victor Stinner97f33c32020-05-14 18:05:58 +020015472 state->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070015473
Victor Stinner97f33c32020-05-14 18:05:58 +020015474 if ((state->billion = PyLong_FromLong(1000000000)) == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015475 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015476#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Victor Stinner97f33c32020-05-14 18:05:58 +020015477 state->struct_rusage = PyUnicode_InternFromString("struct_rusage");
15478 if (state->struct_rusage == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015479 return -1;
Eddie Elizondob3966632019-11-05 07:16:14 -080015480#endif
Victor Stinner97f33c32020-05-14 18:05:58 +020015481 state->st_mode = PyUnicode_InternFromString("st_mode");
15482 if (state->st_mode == NULL)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015483 return -1;
Larry Hastings6fe20b32012-04-19 15:07:49 -070015484
Larry Hastings9cf065c2012-06-22 16:30:09 -070015485 /* suppress "function not used" warnings */
15486 {
15487 int ignored;
15488 fd_specified("", -1);
15489 follow_symlinks_specified("", 1);
15490 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
15491 dir_fd_converter(Py_None, &ignored);
15492 dir_fd_unavailable(Py_None, &ignored);
15493 }
15494
15495 /*
15496 * provide list of locally available functions
15497 * so os.py can populate support_* lists
15498 */
Victor Stinner97f33c32020-05-14 18:05:58 +020015499 PyObject *list = PyList_New(0);
15500 if (!list) {
Victor Stinner1c2fa782020-05-10 11:05:29 +020015501 return -1;
Victor Stinner97f33c32020-05-14 18:05:58 +020015502 }
Ronald Oussoren41761932020-11-08 10:05:27 +010015503 for (const struct have_function *trace = have_functions; trace->label; trace++) {
15504 PyObject *unicode;
15505 if (trace->probe && !trace->probe()) continue;
15506 unicode = PyUnicode_DecodeASCII(trace->label, strlen(trace->label), NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -070015507 if (!unicode)
Victor Stinner1c2fa782020-05-10 11:05:29 +020015508 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -070015509 if (PyList_Append(list, unicode))
Victor Stinner1c2fa782020-05-10 11:05:29 +020015510 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -070015511 Py_DECREF(unicode);
15512 }
Ronald Oussoren41761932020-11-08 10:05:27 +010015513
Larry Hastings9cf065c2012-06-22 16:30:09 -070015514 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040015515
Victor Stinner1c2fa782020-05-10 11:05:29 +020015516 return 0;
15517}
15518
15519
15520static PyModuleDef_Slot posixmodile_slots[] = {
15521 {Py_mod_exec, posixmodule_exec},
15522 {0, NULL}
15523};
15524
15525static struct PyModuleDef posixmodule = {
15526 PyModuleDef_HEAD_INIT,
15527 .m_name = MODNAME,
15528 .m_doc = posix__doc__,
15529 .m_size = sizeof(_posixstate),
15530 .m_methods = posix_methods,
15531 .m_slots = posixmodile_slots,
15532 .m_traverse = _posix_traverse,
15533 .m_clear = _posix_clear,
15534 .m_free = _posix_free,
15535};
15536
15537PyMODINIT_FUNC
15538INITFUNC(void)
15539{
15540 return PyModuleDef_Init(&posixmodule);
Guido van Rossumb6775db1994-08-01 11:34:53 +000015541}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000015542
15543#ifdef __cplusplus
15544}
15545#endif