blob: 3d3f6ac969926ff3535080191d05050f17453849 [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 Wouters477c8d52006-05-27 19:21:47 +000010#ifdef __APPLE__
11 /*
Victor Stinner8c62be82010-05-06 00:08:46 +000012 * Step 1 of support for weak-linking a number of symbols existing on
Thomas Wouters477c8d52006-05-27 19:21:47 +000013 * OSX 10.4 and later, see the comment in the #ifdef __APPLE__ block
14 * at the end of this file for more information.
15 */
16# pragma weak lchown
17# pragma weak statvfs
18# pragma weak fstatvfs
19
20#endif /* __APPLE__ */
21
Thomas Wouters68bc4f92006-03-01 01:05:10 +000022#define PY_SSIZE_T_CLEAN
23
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000024#include "Python.h"
Victor Stinnerd5d9e812019-05-13 12:35:37 +020025#ifdef MS_WINDOWS
26 /* include <windows.h> early to avoid conflict with pycore_condvar.h:
27
28 #define WIN32_LEAN_AND_MEAN
29 #include <windows.h>
30
31 FSCTL_GET_REPARSE_POINT is not exported with WIN32_LEAN_AND_MEAN. */
32# include <windows.h>
33#endif
34
Victor Stinner4a21e572020-04-15 02:35:41 +020035#include "pycore_ceval.h" // _PyEval_ReInitThreads()
36#include "pycore_import.h" // _PyImport_ReInitLock()
37#include "pycore_pystate.h" // _PyInterpreterState_GET()
38#include "structmember.h" // PyMemberDef
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020039#ifndef MS_WINDOWS
Victor Stinnerd5d9e812019-05-13 12:35:37 +020040# include "posixmodule.h"
Tim Golden0321cf22014-05-05 19:46:17 +010041#else
Victor Stinnerd5d9e812019-05-13 12:35:37 +020042# include "winreparse.h"
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020043#endif
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000044
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020045/* On android API level 21, 'AT_EACCESS' is not declared although
46 * HAVE_FACCESSAT is defined. */
47#ifdef __ANDROID__
Victor Stinner5eca75d2020-04-15 15:07:31 +020048# undef HAVE_FACCESSAT
Stefan Krahfb7c8ae2016-04-26 17:04:18 +020049#endif
50
Gregory P. Smith ext:(%20%5BGoogle%20Inc.%5D)fa76eee2016-05-28 21:03:48 +000051#include <stdio.h> /* needed for ctermid() */
52
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000053#ifdef __cplusplus
54extern "C" {
55#endif
56
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000057PyDoc_STRVAR(posix__doc__,
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000058"This module provides access to operating system functionality that is\n\
59standardized by the C Standard and the POSIX standard (a thinly\n\
60disguised Unix interface). Refer to the library manual and\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000061corresponding Unix manual entries for more information on calls.");
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000062
Martin v. Löwis0073f2e2002-11-21 23:52:35 +000063
Ross Lagerwall4d076da2011-03-18 06:56:53 +020064#ifdef HAVE_SYS_UIO_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020065# include <sys/uio.h>
Ross Lagerwall4d076da2011-03-18 06:56:53 +020066#endif
67
Christian Heimes75b96182017-09-05 15:53:09 +020068#ifdef HAVE_SYS_SYSMACROS_H
69/* GNU C Library: major(), minor(), makedev() */
Victor Stinner5eca75d2020-04-15 15:07:31 +020070# include <sys/sysmacros.h>
Christian Heimes75b96182017-09-05 15:53:09 +020071#endif
72
Thomas Wouters0e3f5912006-08-11 14:57:12 +000073#ifdef HAVE_SYS_TYPES_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020074# include <sys/types.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000075#endif /* HAVE_SYS_TYPES_H */
76
77#ifdef HAVE_SYS_STAT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020078# include <sys/stat.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000079#endif /* HAVE_SYS_STAT_H */
Guido van Rossuma6535fd2001-10-18 19:44:10 +000080
Guido van Rossum36bc6801995-06-14 22:54:23 +000081#ifdef HAVE_SYS_WAIT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020082# include <sys/wait.h> // WNOHANG
Guido van Rossum36bc6801995-06-14 22:54:23 +000083#endif
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080084#ifdef HAVE_LINUX_WAIT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020085# include <linux/wait.h> // P_PIDFD
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080086#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000087
Thomas Wouters0e3f5912006-08-11 14:57:12 +000088#ifdef HAVE_SIGNAL_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020089# include <signal.h>
Thomas Wouters0e3f5912006-08-11 14:57:12 +000090#endif
Guido van Rossuma376cc51996-12-05 23:43:35 +000091
Guido van Rossumb6775db1994-08-01 11:34:53 +000092#ifdef HAVE_FCNTL_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020093# include <fcntl.h>
94#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +000095
Guido van Rossuma6535fd2001-10-18 19:44:10 +000096#ifdef HAVE_GRP_H
Victor Stinner5eca75d2020-04-15 15:07:31 +020097# include <grp.h>
Guido van Rossuma6535fd2001-10-18 19:44:10 +000098#endif
99
Barry Warsaw5676bd12003-01-07 20:57:09 +0000100#ifdef HAVE_SYSEXITS_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200101# include <sysexits.h>
102#endif
Barry Warsaw5676bd12003-01-07 20:57:09 +0000103
Anthony Baxter8a560de2004-10-13 15:30:56 +0000104#ifdef HAVE_SYS_LOADAVG_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200105# include <sys/loadavg.h>
Anthony Baxter8a560de2004-10-13 15:30:56 +0000106#endif
107
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000108#ifdef HAVE_SYS_SENDFILE_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200109# include <sys/sendfile.h>
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000110#endif
111
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200112#if defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200113# include <copyfile.h>
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +0200114#endif
115
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500116#ifdef HAVE_SCHED_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200117# include <sched.h>
Benjamin Peterson94b580d2011-08-02 17:30:04 -0500118#endif
119
Pablo Galindoaac4d032019-05-31 19:39:47 +0100120#ifdef HAVE_COPY_FILE_RANGE
Victor Stinner5eca75d2020-04-15 15:07:31 +0200121# include <unistd.h>
Pablo Galindoaac4d032019-05-31 19:39:47 +0100122#endif
123
Benjamin Peterson2dbda072012-03-16 10:12:55 -0500124#if !defined(CPU_ALLOC) && defined(HAVE_SCHED_SETAFFINITY)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200125# undef HAVE_SCHED_SETAFFINITY
Benjamin Peterson7b51b8d2012-03-14 22:28:25 -0500126#endif
127
doko@ubuntu.com4a173bc2014-04-17 19:47:16 +0200128#if defined(HAVE_SYS_XATTR_H) && defined(__GLIBC__) && !defined(__FreeBSD_kernel__) && !defined(__GNU__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200129# define USE_XATTRS
Benjamin Peterson9428d532011-09-14 11:45:52 -0400130#endif
131
132#ifdef USE_XATTRS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200133# include <sys/xattr.h>
Benjamin Peterson799bd802011-08-31 22:15:17 -0400134#endif
135
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000136#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200137# ifdef HAVE_SYS_SOCKET_H
138# include <sys/socket.h>
139# endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +0000140#endif
141
Victor Stinner8b905bd2011-10-25 13:34:04 +0200142#ifdef HAVE_DLFCN_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200143# include <dlfcn.h>
Victor Stinner8b905bd2011-10-25 13:34:04 +0200144#endif
145
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200146#ifdef __hpux
Victor Stinner5eca75d2020-04-15 15:07:31 +0200147# include <sys/mpctl.h>
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200148#endif
149
150#if defined(__DragonFly__) || \
151 defined(__OpenBSD__) || \
152 defined(__FreeBSD__) || \
153 defined(__NetBSD__) || \
154 defined(__APPLE__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200155# include <sys/sysctl.h>
Charles-Francois Natali44feda32013-05-20 14:40:46 +0200156#endif
157
Victor Stinner9b1f4742016-09-06 16:18:52 -0700158#ifdef HAVE_LINUX_RANDOM_H
159# include <linux/random.h>
160#endif
161#ifdef HAVE_GETRANDOM_SYSCALL
162# include <sys/syscall.h>
163#endif
164
Antoine Pitroubcf2b592012-02-08 23:28:36 +0100165#if defined(MS_WINDOWS)
166# define TERMSIZE_USE_CONIO
167#elif defined(HAVE_SYS_IOCTL_H)
168# include <sys/ioctl.h>
169# if defined(HAVE_TERMIOS_H)
170# include <termios.h>
171# endif
172# if defined(TIOCGWINSZ)
173# define TERMSIZE_USE_IOCTL
174# endif
175#endif /* MS_WINDOWS */
176
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000177/* Various compilers have only certain posix functions */
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000178/* XXX Gosh I wish these were all moved into pyconfig.h */
Victor Stinner8c62be82010-05-06 00:08:46 +0000179#if defined(__WATCOMC__) && !defined(__QNX__) /* Watcom compiler */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200180# define HAVE_OPENDIR 1
181# define HAVE_SYSTEM 1
182# include <process.h>
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000183#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200184# ifdef _MSC_VER
185 /* Microsoft compiler */
186# define HAVE_GETPPID 1
187# define HAVE_GETLOGIN 1
188# define HAVE_SPAWNV 1
189# define HAVE_EXECV 1
190# define HAVE_WSPAWNV 1
191# define HAVE_WEXECV 1
192# define HAVE_PIPE 1
193# define HAVE_SYSTEM 1
194# define HAVE_CWAIT 1
195# define HAVE_FSYNC 1
196# define fsync _commit
197# else
198 /* Unix functions that the configure script doesn't check for */
199# ifndef __VXWORKS__
200# define HAVE_EXECV 1
201# define HAVE_FORK 1
202# if defined(__USLC__) && defined(__SCO_VERSION__) /* SCO UDK Compiler */
203# define HAVE_FORK1 1
204# endif
205# endif
206# define HAVE_GETEGID 1
207# define HAVE_GETEUID 1
208# define HAVE_GETGID 1
209# define HAVE_GETPPID 1
210# define HAVE_GETUID 1
211# define HAVE_KILL 1
212# define HAVE_OPENDIR 1
213# define HAVE_PIPE 1
214# define HAVE_SYSTEM 1
215# define HAVE_WAIT 1
216# define HAVE_TTYNAME 1
217# endif /* _MSC_VER */
Guido van Rossumc5a0f531997-12-02 20:36:02 +0000218#endif /* ! __WATCOMC__ || __QNX__ */
Guido van Rossumad0ee831995-03-01 10:34:45 +0000219
Eddie Elizondob3966632019-11-05 07:16:14 -0800220_Py_IDENTIFIER(__fspath__);
Victor Stinnera2f7c002012-02-08 03:36:25 +0100221
Larry Hastings61272b72014-01-07 12:41:53 -0800222/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +1000223# one of the few times we lie about this name!
Larry Hastings44e2eaa2013-11-23 15:37:55 -0800224module os
Larry Hastings61272b72014-01-07 12:41:53 -0800225[clinic start generated code]*/
Larry Hastings2f936352014-08-05 14:04:04 +1000226/*[clinic end generated code: output=da39a3ee5e6b4b0d input=94a0f0f978acae17]*/
Victor Stinnera2f7c002012-02-08 03:36:25 +0100227
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000228#ifndef _MSC_VER
Guido van Rossum36bc6801995-06-14 22:54:23 +0000229
Martin v. Löwis8eb92a02002-09-19 08:03:21 +0000230#if defined(__sgi)&&_COMPILER_VERSION>=700
231/* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode
232 (default) */
233extern char *ctermid_r(char *);
234#endif
235
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000236#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237
pxinwrf2d7ac72019-05-21 18:46:37 +0800238#if defined(__VXWORKS__)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200239# include <vxCpuLib.h>
240# include <rtpLib.h>
241# include <wait.h>
242# include <taskLib.h>
243# ifndef _P_WAIT
244# define _P_WAIT 0
245# define _P_NOWAIT 1
246# define _P_NOWAITO 1
247# endif
pxinwrf2d7ac72019-05-21 18:46:37 +0800248#endif /* __VXWORKS__ */
249
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000250#ifdef HAVE_POSIX_SPAWN
Victor Stinner5eca75d2020-04-15 15:07:31 +0200251# include <spawn.h>
Pablo Galindo6c6ddf92018-01-29 01:56:10 +0000252#endif
253
Guido van Rossumb6775db1994-08-01 11:34:53 +0000254#ifdef HAVE_UTIME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200255# include <utime.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000256#endif /* HAVE_UTIME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000257
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000258#ifdef HAVE_SYS_UTIME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200259# include <sys/utime.h>
260# define HAVE_UTIME_H /* pretend we do for the rest of this file */
Guido van Rossum14ed0b21994-09-29 09:50:09 +0000261#endif /* HAVE_SYS_UTIME_H */
262
Guido van Rossumb6775db1994-08-01 11:34:53 +0000263#ifdef HAVE_SYS_TIMES_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200264# include <sys/times.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000265#endif /* HAVE_SYS_TIMES_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000266
267#ifdef HAVE_SYS_PARAM_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200268# include <sys/param.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000269#endif /* HAVE_SYS_PARAM_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000270
271#ifdef HAVE_SYS_UTSNAME_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200272# include <sys/utsname.h>
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +0000273#endif /* HAVE_SYS_UTSNAME_H */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000274
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000275#ifdef HAVE_DIRENT_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200276# include <dirent.h>
277# define NAMLEN(dirent) strlen((dirent)->d_name)
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000278#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200279# if defined(__WATCOMC__) && !defined(__QNX__)
280# include <direct.h>
281# define NAMLEN(dirent) strlen((dirent)->d_name)
282# else
283# define dirent direct
284# define NAMLEN(dirent) (dirent)->d_namlen
285# endif
286# ifdef HAVE_SYS_NDIR_H
287# include <sys/ndir.h>
288# endif
289# ifdef HAVE_SYS_DIR_H
290# include <sys/dir.h>
291# endif
292# ifdef HAVE_NDIR_H
293# include <ndir.h>
294# endif
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000295#endif
Guido van Rossumb6775db1994-08-01 11:34:53 +0000296
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000297#ifdef _MSC_VER
Victor Stinner5eca75d2020-04-15 15:07:31 +0200298# ifdef HAVE_DIRECT_H
299# include <direct.h>
300# endif
301# ifdef HAVE_IO_H
302# include <io.h>
303# endif
304# ifdef HAVE_PROCESS_H
305# include <process.h>
306# endif
307# ifndef IO_REPARSE_TAG_SYMLINK
308# define IO_REPARSE_TAG_SYMLINK (0xA000000CL)
309# endif
310# ifndef IO_REPARSE_TAG_MOUNT_POINT
311# define IO_REPARSE_TAG_MOUNT_POINT (0xA0000003L)
312# endif
313# include "osdefs.h" // SEP
314# include <malloc.h>
315# include <windows.h>
316# include <shellapi.h> // ShellExecute()
317# include <lmcons.h> // UNLEN
318# define HAVE_SYMLINK
Guido van Rossuma4916fa1996-05-23 22:58:55 +0000319#endif /* _MSC_VER */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000320
Tim Petersbc2e10e2002-03-03 23:17:02 +0000321#ifndef MAXPATHLEN
Victor Stinner5eca75d2020-04-15 15:07:31 +0200322# if defined(PATH_MAX) && PATH_MAX > 1024
323# define MAXPATHLEN PATH_MAX
324# else
325# define MAXPATHLEN 1024
326# endif
Tim Petersbc2e10e2002-03-03 23:17:02 +0000327#endif /* MAXPATHLEN */
328
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000329#ifdef UNION_WAIT
Victor Stinner5eca75d2020-04-15 15:07:31 +0200330 /* Emulate some macros on systems that have a union instead of macros */
331# ifndef WIFEXITED
332# define WIFEXITED(u_wait) (!(u_wait).w_termsig && !(u_wait).w_coredump)
333# endif
334# ifndef WEXITSTATUS
335# define WEXITSTATUS(u_wait) (WIFEXITED(u_wait)?((u_wait).w_retcode):-1)
336# endif
337# ifndef WTERMSIG
338# define WTERMSIG(u_wait) ((u_wait).w_termsig)
339# endif
340# define WAIT_TYPE union wait
341# define WAIT_STATUS_INT(s) (s.w_status)
342#else
343 /* !UNION_WAIT */
344# define WAIT_TYPE int
345# define WAIT_STATUS_INT(s) (s)
Guido van Rossum54ecc3d1999-01-27 17:53:11 +0000346#endif /* UNION_WAIT */
347
Greg Wardb48bc172000-03-01 21:51:56 +0000348/* Don't use the "_r" form if we don't need it (also, won't have a
349 prototype for it, at least on Solaris -- maybe others as well?). */
Antoine Pitroua6a4dc82017-09-07 18:56:24 +0200350#if defined(HAVE_CTERMID_R)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200351# define USE_CTERMID_R
Greg Wardb48bc172000-03-01 21:51:56 +0000352#endif
353
Fred Drake699f3522000-06-29 21:12:41 +0000354/* choose the appropriate stat and fstat functions and return structs */
Guido van Rossum64529cd2000-06-30 22:45:12 +0000355#undef STAT
Antoine Pitroue47e0932011-01-19 15:21:35 +0000356#undef FSTAT
357#undef STRUCT_STAT
Victor Stinner14b9b112013-06-25 00:37:25 +0200358#ifdef MS_WINDOWS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200359# define STAT win32_stat
360# define LSTAT win32_lstat
361# define FSTAT _Py_fstat_noraise
362# define STRUCT_STAT struct _Py_stat_struct
Fred Drake699f3522000-06-29 21:12:41 +0000363#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200364# define STAT stat
365# define LSTAT lstat
366# define FSTAT fstat
367# define STRUCT_STAT struct stat
Fred Drake699f3522000-06-29 21:12:41 +0000368#endif
369
Tim Peters11b23062003-04-23 02:39:17 +0000370#if defined(MAJOR_IN_MKDEV)
Victor Stinner5eca75d2020-04-15 15:07:31 +0200371# include <sys/mkdev.h>
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000372#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200373# if defined(MAJOR_IN_SYSMACROS)
374# include <sys/sysmacros.h>
375# endif
376# if defined(HAVE_MKNOD) && defined(HAVE_SYS_MKDEV_H)
377# include <sys/mkdev.h>
378# endif
Martin v. Löwisdbe3f762002-10-10 14:27:30 +0000379#endif
Fred Drake699f3522000-06-29 21:12:41 +0000380
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200381#ifdef MS_WINDOWS
Victor Stinner5eca75d2020-04-15 15:07:31 +0200382# define INITFUNC PyInit_nt
383# define MODNAME "nt"
Victor Stinner6036e442015-03-08 01:58:04 +0100384#else
Victor Stinner5eca75d2020-04-15 15:07:31 +0200385# define INITFUNC PyInit_posix
386# define MODNAME "posix"
Victor Stinner6036e442015-03-08 01:58:04 +0100387#endif
388
jcea6c51d512018-01-28 14:00:08 +0100389#if defined(__sun)
390/* Something to implement in autoconf, not present in autoconf 2.69 */
Victor Stinner5eca75d2020-04-15 15:07:31 +0200391# define HAVE_STRUCT_STAT_ST_FSTYPE 1
jcea6c51d512018-01-28 14:00:08 +0100392#endif
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200393
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600394/* memfd_create is either defined in sys/mman.h or sys/memfd.h
395 * linux/memfd.h defines additional flags
396 */
397#ifdef HAVE_SYS_MMAN_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200398# include <sys/mman.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600399#endif
400#ifdef HAVE_SYS_MEMFD_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200401# include <sys/memfd.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600402#endif
403#ifdef HAVE_LINUX_MEMFD_H
Victor Stinner5eca75d2020-04-15 15:07:31 +0200404# include <linux/memfd.h>
Zackery Spytz43fdbd22019-05-29 13:57:07 -0600405#endif
406
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800407#ifdef _Py_MEMORY_SANITIZER
Victor Stinner5eca75d2020-04-15 15:07:31 +0200408# include <sanitizer/msan_interface.h>
Gregory P. Smith1d300ce2018-12-30 21:13:02 -0800409#endif
410
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200411#ifdef HAVE_FORK
412static void
413run_at_forkers(PyObject *lst, int reverse)
414{
415 Py_ssize_t i;
416 PyObject *cpy;
417
418 if (lst != NULL) {
419 assert(PyList_CheckExact(lst));
420
421 /* Use a list copy in case register_at_fork() is called from
422 * one of the callbacks.
423 */
424 cpy = PyList_GetSlice(lst, 0, PyList_GET_SIZE(lst));
425 if (cpy == NULL)
426 PyErr_WriteUnraisable(lst);
427 else {
428 if (reverse)
429 PyList_Reverse(cpy);
430 for (i = 0; i < PyList_GET_SIZE(cpy); i++) {
431 PyObject *func, *res;
432 func = PyList_GET_ITEM(cpy, i);
Jeroen Demeyer7f41c8e2019-07-04 12:35:31 +0200433 res = _PyObject_CallNoArg(func);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200434 if (res == NULL)
435 PyErr_WriteUnraisable(func);
436 else
437 Py_DECREF(res);
438 }
439 Py_DECREF(cpy);
440 }
441 }
442}
443
444void
445PyOS_BeforeFork(void)
446{
Victor Stinner81a7be32020-04-14 15:14:01 +0200447 run_at_forkers(_PyInterpreterState_GET()->before_forkers, 1);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200448
449 _PyImport_AcquireLock();
450}
451
452void
453PyOS_AfterFork_Parent(void)
454{
455 if (_PyImport_ReleaseLock() <= 0)
456 Py_FatalError("failed releasing import lock after fork");
457
Victor Stinner81a7be32020-04-14 15:14:01 +0200458 run_at_forkers(_PyInterpreterState_GET()->after_forkers_parent, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200459}
460
461void
462PyOS_AfterFork_Child(void)
463{
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200464 _PyRuntimeState *runtime = &_PyRuntime;
465 _PyGILState_Reinit(runtime);
Victor Stinnerd5d9e812019-05-13 12:35:37 +0200466 _PyEval_ReInitThreads(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200467 _PyImport_ReInitLock();
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200468 _PySignal_AfterFork();
Victor Stinnerb930a2d2019-04-24 17:14:33 +0200469 _PyRuntimeState_ReInitThreads(runtime);
Victor Stinnerb49858b2019-05-24 15:20:23 +0200470 _PyInterpreterState_DeleteExceptMain(runtime);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200471
Victor Stinner81a7be32020-04-14 15:14:01 +0200472 run_at_forkers(_PyInterpreterState_GET()->after_forkers_child, 0);
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200473}
474
475static int
476register_at_forker(PyObject **lst, PyObject *func)
477{
Gregory P. Smith163468a2017-05-29 10:03:41 -0700478 if (func == NULL) /* nothing to register? do nothing. */
479 return 0;
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200480 if (*lst == NULL) {
481 *lst = PyList_New(0);
482 if (*lst == NULL)
483 return -1;
484 }
485 return PyList_Append(*lst, func);
486}
Victor Stinner87255be2020-04-07 23:11:49 +0200487#endif /* HAVE_FORK */
488
Antoine Pitrou346cbd32017-05-27 17:50:54 +0200489
490/* Legacy wrapper */
491void
492PyOS_AfterFork(void)
493{
494#ifdef HAVE_FORK
495 PyOS_AfterFork_Child();
496#endif
497}
498
499
Victor Stinner6036e442015-03-08 01:58:04 +0100500#ifdef MS_WINDOWS
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200501/* defined in fileutils.c */
Benjamin Petersone5024512018-09-12 12:06:42 -0700502void _Py_time_t_to_FILE_TIME(time_t, int, FILETIME *);
503void _Py_attribute_data_to_stat(BY_HANDLE_FILE_INFORMATION *,
Serhiy Storchaka06a13f82015-02-22 21:34:54 +0200504 ULONG, struct _Py_stat_struct *);
505#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700506
Larry Hastings9cf065c2012-06-22 16:30:09 -0700507
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200508#ifndef MS_WINDOWS
509PyObject *
510_PyLong_FromUid(uid_t uid)
511{
512 if (uid == (uid_t)-1)
513 return PyLong_FromLong(-1);
514 return PyLong_FromUnsignedLong(uid);
515}
516
517PyObject *
518_PyLong_FromGid(gid_t gid)
519{
520 if (gid == (gid_t)-1)
521 return PyLong_FromLong(-1);
522 return PyLong_FromUnsignedLong(gid);
523}
524
525int
526_Py_Uid_Converter(PyObject *obj, void *p)
527{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700528 uid_t uid;
529 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200530 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200531 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700532 unsigned long uresult;
533
534 index = PyNumber_Index(obj);
535 if (index == NULL) {
536 PyErr_Format(PyExc_TypeError,
537 "uid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800538 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200539 return 0;
540 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700541
542 /*
543 * Handling uid_t is complicated for two reasons:
544 * * Although uid_t is (always?) unsigned, it still
545 * accepts -1.
546 * * We don't know its size in advance--it may be
547 * bigger than an int, or it may be smaller than
548 * a long.
549 *
550 * So a bit of defensive programming is in order.
551 * Start with interpreting the value passed
552 * in as a signed long and see if it works.
553 */
554
555 result = PyLong_AsLongAndOverflow(index, &overflow);
556
557 if (!overflow) {
558 uid = (uid_t)result;
559
560 if (result == -1) {
561 if (PyErr_Occurred())
562 goto fail;
563 /* It's a legitimate -1, we're done. */
564 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200565 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700566
567 /* Any other negative number is disallowed. */
568 if (result < 0)
569 goto underflow;
570
571 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200572 if (sizeof(uid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700573 (long)uid != result)
574 goto underflow;
575 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200576 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700577
578 if (overflow < 0)
579 goto underflow;
580
581 /*
582 * Okay, the value overflowed a signed long. If it
583 * fits in an *unsigned* long, it may still be okay,
584 * as uid_t may be unsigned long on this platform.
585 */
586 uresult = PyLong_AsUnsignedLong(index);
587 if (PyErr_Occurred()) {
588 if (PyErr_ExceptionMatches(PyExc_OverflowError))
589 goto overflow;
590 goto fail;
591 }
592
593 uid = (uid_t)uresult;
594
595 /*
596 * If uid == (uid_t)-1, the user actually passed in ULONG_MAX,
597 * but this value would get interpreted as (uid_t)-1 by chown
598 * and its siblings. That's not what the user meant! So we
599 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100600 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700601 */
602 if (uid == (uid_t)-1)
603 goto overflow;
604
605 /* Ensure the value wasn't truncated. */
606 if (sizeof(uid_t) < sizeof(long) &&
607 (unsigned long)uid != uresult)
608 goto overflow;
609 /* fallthrough */
610
611success:
612 Py_DECREF(index);
613 *(uid_t *)p = uid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200614 return 1;
615
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700616underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200617 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700618 "uid is less than minimum");
619 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200620
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700621overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200622 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700623 "uid is greater than maximum");
624 /* fallthrough */
625
626fail:
627 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200628 return 0;
629}
630
631int
632_Py_Gid_Converter(PyObject *obj, void *p)
633{
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700634 gid_t gid;
635 PyObject *index;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200636 int overflow;
Serhiy Storchakab4621892013-02-10 23:28:02 +0200637 long result;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700638 unsigned long uresult;
639
640 index = PyNumber_Index(obj);
641 if (index == NULL) {
642 PyErr_Format(PyExc_TypeError,
643 "gid should be integer, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800644 _PyType_Name(Py_TYPE(obj)));
Serhiy Storchakab4621892013-02-10 23:28:02 +0200645 return 0;
646 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700647
648 /*
649 * Handling gid_t is complicated for two reasons:
650 * * Although gid_t is (always?) unsigned, it still
651 * accepts -1.
652 * * We don't know its size in advance--it may be
653 * bigger than an int, or it may be smaller than
654 * a long.
655 *
656 * So a bit of defensive programming is in order.
657 * Start with interpreting the value passed
658 * in as a signed long and see if it works.
659 */
660
661 result = PyLong_AsLongAndOverflow(index, &overflow);
662
663 if (!overflow) {
664 gid = (gid_t)result;
665
666 if (result == -1) {
667 if (PyErr_Occurred())
668 goto fail;
669 /* It's a legitimate -1, we're done. */
670 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200671 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700672
673 /* Any other negative number is disallowed. */
674 if (result < 0) {
675 goto underflow;
676 }
677
678 /* Ensure the value wasn't truncated. */
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200679 if (sizeof(gid_t) < sizeof(long) &&
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700680 (long)gid != result)
681 goto underflow;
682 goto success;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200683 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700684
685 if (overflow < 0)
686 goto underflow;
687
688 /*
689 * Okay, the value overflowed a signed long. If it
690 * fits in an *unsigned* long, it may still be okay,
691 * as gid_t may be unsigned long on this platform.
692 */
693 uresult = PyLong_AsUnsignedLong(index);
694 if (PyErr_Occurred()) {
695 if (PyErr_ExceptionMatches(PyExc_OverflowError))
696 goto overflow;
697 goto fail;
698 }
699
700 gid = (gid_t)uresult;
701
702 /*
703 * If gid == (gid_t)-1, the user actually passed in ULONG_MAX,
704 * but this value would get interpreted as (gid_t)-1 by chown
705 * and its siblings. That's not what the user meant! So we
706 * throw an overflow exception instead. (We already
Tim Golden23005082013-10-25 11:22:37 +0100707 * handled a real -1 with PyLong_AsLongAndOverflow() above.)
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700708 */
709 if (gid == (gid_t)-1)
710 goto overflow;
711
712 /* Ensure the value wasn't truncated. */
713 if (sizeof(gid_t) < sizeof(long) &&
714 (unsigned long)gid != uresult)
715 goto overflow;
716 /* fallthrough */
717
718success:
719 Py_DECREF(index);
720 *(gid_t *)p = gid;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200721 return 1;
722
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700723underflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200724 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700725 "gid is less than minimum");
726 goto fail;
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200727
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700728overflow:
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200729 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700730 "gid is greater than maximum");
731 /* fallthrough */
732
733fail:
734 Py_DECREF(index);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +0200735 return 0;
736}
737#endif /* MS_WINDOWS */
738
739
Benjamin Petersoned4aa832016-09-05 17:44:18 -0700740#define _PyLong_FromDev PyLong_FromLongLong
Gregory P. Smith702dada2015-01-28 16:07:52 -0800741
742
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200743#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
744static int
745_Py_Dev_Converter(PyObject *obj, void *p)
746{
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200747 *((dev_t *)p) = PyLong_AsUnsignedLongLong(obj);
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200748 if (PyErr_Occurred())
749 return 0;
750 return 1;
751}
Gregory P. Smith702dada2015-01-28 16:07:52 -0800752#endif /* HAVE_MKNOD && HAVE_MAKEDEV */
Serhiy Storchakab2653b32015-01-18 11:12:11 +0200753
754
Larry Hastings9cf065c2012-06-22 16:30:09 -0700755#ifdef AT_FDCWD
Trent Nelson9a461052012-09-18 21:50:06 -0400756/*
757 * Why the (int) cast? Solaris 10 defines AT_FDCWD as 0xffd19553 (-3041965);
758 * without the int cast, the value gets interpreted as uint (4291925331),
759 * which doesn't play nicely with all the initializer lines in this file that
760 * look like this:
761 * int dir_fd = DEFAULT_DIR_FD;
762 */
763#define DEFAULT_DIR_FD (int)AT_FDCWD
Larry Hastings9cf065c2012-06-22 16:30:09 -0700764#else
765#define DEFAULT_DIR_FD (-100)
766#endif
767
768static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300769_fd_converter(PyObject *o, int *p)
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200770{
771 int overflow;
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700772 long long_value;
773
774 PyObject *index = PyNumber_Index(o);
775 if (index == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700776 return 0;
777 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700778
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300779 assert(PyLong_Check(index));
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700780 long_value = PyLong_AsLongAndOverflow(index, &overflow);
781 Py_DECREF(index);
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300782 assert(!PyErr_Occurred());
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200783 if (overflow > 0 || long_value > INT_MAX) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700784 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700785 "fd is greater than maximum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700786 return 0;
787 }
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200788 if (overflow < 0 || long_value < INT_MIN) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700789 PyErr_SetString(PyExc_OverflowError,
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700790 "fd is less than minimum");
Larry Hastings9cf065c2012-06-22 16:30:09 -0700791 return 0;
792 }
Larry Hastingsa27b83a2013-08-08 00:19:50 -0700793
Larry Hastings9cf065c2012-06-22 16:30:09 -0700794 *p = (int)long_value;
795 return 1;
796}
797
798static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +0200799dir_fd_converter(PyObject *o, void *p)
800{
801 if (o == Py_None) {
802 *(int *)p = DEFAULT_DIR_FD;
803 return 1;
804 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300805 else if (PyIndex_Check(o)) {
806 return _fd_converter(o, (int *)p);
807 }
808 else {
809 PyErr_Format(PyExc_TypeError,
810 "argument should be integer or None, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -0800811 _PyType_Name(Py_TYPE(o)));
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300812 return 0;
813 }
Larry Hastings9cf065c2012-06-22 16:30:09 -0700814}
815
Eddie Elizondob3966632019-11-05 07:16:14 -0800816typedef struct {
817 PyObject *billion;
Eddie Elizondob3966632019-11-05 07:16:14 -0800818 PyObject *DirEntryType;
819 PyObject *ScandirIteratorType;
820#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
821 PyObject *SchedParamType;
822#endif
823 PyObject *StatResultType;
824 PyObject *StatVFSResultType;
825 PyObject *TerminalSizeType;
826 PyObject *TimesResultType;
827 PyObject *UnameResultType;
828#if defined(HAVE_WAITID) && !defined(__APPLE__)
829 PyObject *WaitidResultType;
830#endif
831#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
832 PyObject *struct_rusage;
833#endif
834 PyObject *st_mode;
835} _posixstate;
836
837static struct PyModuleDef posixmodule;
838
Hai Shif707d942020-03-16 21:15:01 +0800839static inline _posixstate*
840get_posix_state(PyObject *module)
841{
842 void *state = PyModule_GetState(module);
843 assert(state != NULL);
844 return (_posixstate *)state;
845}
846
Eddie Elizondob3966632019-11-05 07:16:14 -0800847#define _posixstate_global ((_posixstate *)PyModule_GetState(PyState_FindModule(&posixmodule)))
Larry Hastings9cf065c2012-06-22 16:30:09 -0700848
Larry Hastings9cf065c2012-06-22 16:30:09 -0700849/*
850 * A PyArg_ParseTuple "converter" function
851 * that handles filesystem paths in the manner
852 * preferred by the os module.
853 *
854 * path_converter accepts (Unicode) strings and their
855 * subclasses, and bytes and their subclasses. What
856 * it does with the argument depends on the platform:
857 *
858 * * On Windows, if we get a (Unicode) string we
859 * extract the wchar_t * and return it; if we get
Steve Dowercc16be82016-09-08 10:35:16 -0700860 * bytes we decode to wchar_t * and return that.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700861 *
862 * * On all other platforms, strings are encoded
863 * to bytes using PyUnicode_FSConverter, then we
864 * extract the char * from the bytes object and
865 * return that.
866 *
867 * path_converter also optionally accepts signed
868 * integers (representing open file descriptors) instead
869 * of path strings.
870 *
871 * Input fields:
872 * path.nullable
873 * If nonzero, the path is permitted to be None.
874 * path.allow_fd
875 * If nonzero, the path is permitted to be a file handle
876 * (a signed int) instead of a string.
877 * path.function_name
878 * If non-NULL, path_converter will use that as the name
879 * of the function in error messages.
Larry Hastings31826802013-10-19 00:09:25 -0700880 * (If path.function_name is NULL it omits the function name.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700881 * path.argument_name
882 * If non-NULL, path_converter will use that as the name
883 * of the parameter in error messages.
884 * (If path.argument_name is NULL it uses "path".)
885 *
886 * Output fields:
887 * path.wide
888 * Points to the path if it was expressed as Unicode
889 * and was not encoded. (Only used on Windows.)
890 * path.narrow
891 * Points to the path if it was expressed as bytes,
Steve Dowercc16be82016-09-08 10:35:16 -0700892 * or it was Unicode and was encoded to bytes. (On Windows,
Martin Panterb1321fb2016-10-10 00:38:21 +0000893 * is a non-zero integer if the path was expressed as bytes.
Steve Dowercc16be82016-09-08 10:35:16 -0700894 * The type is deliberately incompatible to prevent misuse.)
Larry Hastings9cf065c2012-06-22 16:30:09 -0700895 * path.fd
896 * Contains a file descriptor if path.accept_fd was true
897 * and the caller provided a signed integer instead of any
898 * sort of string.
899 *
900 * WARNING: if your "path" parameter is optional, and is
901 * unspecified, path_converter will never get called.
902 * So if you set allow_fd, you *MUST* initialize path.fd = -1
903 * yourself!
904 * path.length
905 * The length of the path in characters, if specified as
906 * a string.
907 * path.object
Xiang Zhang04316c42017-01-08 23:26:57 +0800908 * The original object passed in (if get a PathLike object,
909 * the result of PyOS_FSPath() is treated as the original object).
910 * Own a reference to the object.
Larry Hastings9cf065c2012-06-22 16:30:09 -0700911 * path.cleanup
912 * For internal use only. May point to a temporary object.
913 * (Pay no attention to the man behind the curtain.)
914 *
915 * At most one of path.wide or path.narrow will be non-NULL.
916 * If path was None and path.nullable was set,
917 * or if path was an integer and path.allow_fd was set,
918 * both path.wide and path.narrow will be NULL
919 * and path.length will be 0.
Georg Brandlf7875592012-06-24 13:58:31 +0200920 *
Larry Hastings9cf065c2012-06-22 16:30:09 -0700921 * path_converter takes care to not write to the path_t
922 * unless it's successful. However it must reset the
923 * "cleanup" field each time it's called.
924 *
925 * Use as follows:
926 * path_t path;
927 * memset(&path, 0, sizeof(path));
928 * PyArg_ParseTuple(args, "O&", path_converter, &path);
929 * // ... use values from path ...
930 * path_cleanup(&path);
931 *
932 * (Note that if PyArg_Parse fails you don't need to call
933 * path_cleanup(). However it is safe to do so.)
934 */
935typedef struct {
Victor Stinner292c8352012-10-30 02:17:38 +0100936 const char *function_name;
937 const char *argument_name;
Larry Hastings9cf065c2012-06-22 16:30:09 -0700938 int nullable;
939 int allow_fd;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300940 const wchar_t *wide;
Steve Dowercc16be82016-09-08 10:35:16 -0700941#ifdef MS_WINDOWS
942 BOOL narrow;
943#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300944 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700945#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700946 int fd;
947 Py_ssize_t length;
948 PyObject *object;
949 PyObject *cleanup;
950} path_t;
951
Steve Dowercc16be82016-09-08 10:35:16 -0700952#ifdef MS_WINDOWS
953#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
954 {function_name, argument_name, nullable, allow_fd, NULL, FALSE, -1, 0, NULL, NULL}
955#else
Larry Hastings2f936352014-08-05 14:04:04 +1000956#define PATH_T_INITIALIZE(function_name, argument_name, nullable, allow_fd) \
957 {function_name, argument_name, nullable, allow_fd, NULL, NULL, -1, 0, NULL, NULL}
Steve Dowercc16be82016-09-08 10:35:16 -0700958#endif
Larry Hastings31826802013-10-19 00:09:25 -0700959
Larry Hastings9cf065c2012-06-22 16:30:09 -0700960static void
Xiang Zhang04316c42017-01-08 23:26:57 +0800961path_cleanup(path_t *path)
962{
963 Py_CLEAR(path->object);
964 Py_CLEAR(path->cleanup);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700965}
966
967static int
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300968path_converter(PyObject *o, void *p)
969{
Larry Hastings9cf065c2012-06-22 16:30:09 -0700970 path_t *path = (path_t *)p;
Xiang Zhang04316c42017-01-08 23:26:57 +0800971 PyObject *bytes = NULL;
972 Py_ssize_t length = 0;
Brett Cannon3f9183b2016-08-26 14:44:48 -0700973 int is_index, is_buffer, is_bytes, is_unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +0300974 const char *narrow;
Steve Dowercc16be82016-09-08 10:35:16 -0700975#ifdef MS_WINDOWS
Xiang Zhang04316c42017-01-08 23:26:57 +0800976 PyObject *wo = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700977 const wchar_t *wide;
978#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -0700979
980#define FORMAT_EXCEPTION(exc, fmt) \
981 PyErr_Format(exc, "%s%s" fmt, \
982 path->function_name ? path->function_name : "", \
983 path->function_name ? ": " : "", \
984 path->argument_name ? path->argument_name : "path")
985
986 /* Py_CLEANUP_SUPPORTED support */
987 if (o == NULL) {
988 path_cleanup(path);
989 return 1;
990 }
991
Brett Cannon3f9183b2016-08-26 14:44:48 -0700992 /* Ensure it's always safe to call path_cleanup(). */
Xiang Zhang04316c42017-01-08 23:26:57 +0800993 path->object = path->cleanup = NULL;
994 /* path->object owns a reference to the original object */
995 Py_INCREF(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -0700996
Serhiy Storchaka819399b2016-04-06 22:17:52 +0300997 if ((o == Py_None) && path->nullable) {
Larry Hastings9cf065c2012-06-22 16:30:09 -0700998 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -0700999#ifdef MS_WINDOWS
1000 path->narrow = FALSE;
1001#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001002 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001003#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07001004 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001005 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001006 }
1007
Brett Cannon3f9183b2016-08-26 14:44:48 -07001008 /* Only call this here so that we don't treat the return value of
1009 os.fspath() as an fd or buffer. */
1010 is_index = path->allow_fd && PyIndex_Check(o);
1011 is_buffer = PyObject_CheckBuffer(o);
1012 is_bytes = PyBytes_Check(o);
1013 is_unicode = PyUnicode_Check(o);
1014
1015 if (!is_index && !is_buffer && !is_unicode && !is_bytes) {
1016 /* Inline PyOS_FSPath() for better error messages. */
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001017 PyObject *func, *res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001018
1019 func = _PyObject_LookupSpecial(o, &PyId___fspath__);
1020 if (NULL == func) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001021 goto error_format;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001022 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001023 res = _PyObject_CallNoArg(func);
Brett Cannon3f9183b2016-08-26 14:44:48 -07001024 Py_DECREF(func);
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001025 if (NULL == res) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001026 goto error_exit;
1027 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001028 else if (PyUnicode_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001029 is_unicode = 1;
1030 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001031 else if (PyBytes_Check(res)) {
Brett Cannon3f9183b2016-08-26 14:44:48 -07001032 is_bytes = 1;
1033 }
1034 else {
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001035 PyErr_Format(PyExc_TypeError,
1036 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -08001037 "not %.200s", _PyType_Name(Py_TYPE(o)),
1038 _PyType_Name(Py_TYPE(res)));
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001039 Py_DECREF(res);
1040 goto error_exit;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001041 }
Pablo Galindo09fbcd62019-02-18 10:46:34 +00001042
1043 /* still owns a reference to the original object */
1044 Py_DECREF(o);
1045 o = res;
Brett Cannon3f9183b2016-08-26 14:44:48 -07001046 }
1047
1048 if (is_unicode) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001049#ifdef MS_WINDOWS
Victor Stinner26c03bd2016-09-19 11:55:44 +02001050 wide = PyUnicode_AsUnicodeAndSize(o, &length);
Victor Stinner59799a82013-11-13 14:17:30 +01001051 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001052 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001053 }
Victor Stinner59799a82013-11-13 14:17:30 +01001054 if (length > 32767) {
1055 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001056 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001057 }
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001058 if (wcslen(wide) != length) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001059 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001060 goto error_exit;
Serhiy Storchaka2b0d2002015-04-20 09:53:58 +03001061 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001062
1063 path->wide = wide;
Xiang Zhang04316c42017-01-08 23:26:57 +08001064 path->narrow = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001065 path->fd = -1;
Xiang Zhang04316c42017-01-08 23:26:57 +08001066 goto success_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001067#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001068 if (!PyUnicode_FSConverter(o, &bytes)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001069 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001070 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07001071#endif
1072 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001073 else if (is_bytes) {
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001074 bytes = o;
1075 Py_INCREF(bytes);
1076 }
Brett Cannon3f9183b2016-08-26 14:44:48 -07001077 else if (is_buffer) {
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03001078 /* XXX Replace PyObject_CheckBuffer with PyBytes_Check in other code
Ville Skyttä49b27342017-08-03 09:00:59 +03001079 after removing support of non-bytes buffer objects. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001080 if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
1081 "%s%s%s should be %s, not %.200s",
1082 path->function_name ? path->function_name : "",
1083 path->function_name ? ": " : "",
1084 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001085 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1086 "integer or None" :
1087 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1088 path->nullable ? "string, bytes, os.PathLike or None" :
1089 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001090 _PyType_Name(Py_TYPE(o)))) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001091 goto error_exit;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001092 }
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001093 bytes = PyBytes_FromObject(o);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001094 if (!bytes) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001095 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001096 }
1097 }
Steve Dowercc16be82016-09-08 10:35:16 -07001098 else if (is_index) {
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001099 if (!_fd_converter(o, &path->fd)) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001100 goto error_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001101 }
1102 path->wide = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001103#ifdef MS_WINDOWS
1104 path->narrow = FALSE;
1105#else
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001106 path->narrow = NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07001107#endif
Xiang Zhang04316c42017-01-08 23:26:57 +08001108 goto success_exit;
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001109 }
1110 else {
Xiang Zhang04316c42017-01-08 23:26:57 +08001111 error_format:
Serhiy Storchaka819399b2016-04-06 22:17:52 +03001112 PyErr_Format(PyExc_TypeError, "%s%s%s should be %s, not %.200s",
1113 path->function_name ? path->function_name : "",
1114 path->function_name ? ": " : "",
1115 path->argument_name ? path->argument_name : "path",
Brett Cannon3f9183b2016-08-26 14:44:48 -07001116 path->allow_fd && path->nullable ? "string, bytes, os.PathLike, "
1117 "integer or None" :
1118 path->allow_fd ? "string, bytes, os.PathLike or integer" :
1119 path->nullable ? "string, bytes, os.PathLike or None" :
1120 "string, bytes or os.PathLike",
Eddie Elizondob3966632019-11-05 07:16:14 -08001121 _PyType_Name(Py_TYPE(o)));
Xiang Zhang04316c42017-01-08 23:26:57 +08001122 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001123 }
1124
Larry Hastings9cf065c2012-06-22 16:30:09 -07001125 length = PyBytes_GET_SIZE(bytes);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001126 narrow = PyBytes_AS_STRING(bytes);
Victor Stinner706768c2014-08-16 01:03:39 +02001127 if ((size_t)length != strlen(narrow)) {
Serhiy Storchakad8a14472014-09-06 20:07:17 +03001128 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001129 goto error_exit;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001130 }
1131
Steve Dowercc16be82016-09-08 10:35:16 -07001132#ifdef MS_WINDOWS
1133 wo = PyUnicode_DecodeFSDefaultAndSize(
1134 narrow,
1135 length
1136 );
1137 if (!wo) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001138 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001139 }
1140
Xiang Zhang04316c42017-01-08 23:26:57 +08001141 wide = PyUnicode_AsUnicodeAndSize(wo, &length);
Steve Dowercc16be82016-09-08 10:35:16 -07001142 if (!wide) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001143 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001144 }
1145 if (length > 32767) {
1146 FORMAT_EXCEPTION(PyExc_ValueError, "%s too long for Windows");
Xiang Zhang04316c42017-01-08 23:26:57 +08001147 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001148 }
1149 if (wcslen(wide) != length) {
1150 FORMAT_EXCEPTION(PyExc_ValueError, "embedded null character in %s");
Xiang Zhang04316c42017-01-08 23:26:57 +08001151 goto error_exit;
Steve Dowercc16be82016-09-08 10:35:16 -07001152 }
1153 path->wide = wide;
1154 path->narrow = TRUE;
Xiang Zhang04316c42017-01-08 23:26:57 +08001155 path->cleanup = wo;
1156 Py_DECREF(bytes);
Steve Dowercc16be82016-09-08 10:35:16 -07001157#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07001158 path->wide = NULL;
1159 path->narrow = narrow;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001160 if (bytes == o) {
Xiang Zhang04316c42017-01-08 23:26:57 +08001161 /* Still a reference owned by path->object, don't have to
1162 worry about path->narrow is used after free. */
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001163 Py_DECREF(bytes);
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001164 }
1165 else {
1166 path->cleanup = bytes;
Serhiy Storchakad73c3182016-08-06 23:22:08 +03001167 }
Xiang Zhang04316c42017-01-08 23:26:57 +08001168#endif
1169 path->fd = -1;
1170
1171 success_exit:
1172 path->length = length;
1173 path->object = o;
1174 return Py_CLEANUP_SUPPORTED;
1175
1176 error_exit:
1177 Py_XDECREF(o);
1178 Py_XDECREF(bytes);
1179#ifdef MS_WINDOWS
1180 Py_XDECREF(wo);
1181#endif
1182 return 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001183}
1184
1185static void
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001186argument_unavailable_error(const char *function_name, const char *argument_name)
1187{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001188 PyErr_Format(PyExc_NotImplementedError,
1189 "%s%s%s unavailable on this platform",
1190 (function_name != NULL) ? function_name : "",
1191 (function_name != NULL) ? ": ": "",
1192 argument_name);
1193}
1194
1195static int
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001196dir_fd_unavailable(PyObject *o, void *p)
1197{
1198 int dir_fd;
1199 if (!dir_fd_converter(o, &dir_fd))
Larry Hastings9cf065c2012-06-22 16:30:09 -07001200 return 0;
Serhiy Storchakaa2ad5c32013-01-07 23:13:46 +02001201 if (dir_fd != DEFAULT_DIR_FD) {
1202 argument_unavailable_error(NULL, "dir_fd");
1203 return 0;
1204 }
1205 *(int *)p = dir_fd;
1206 return 1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07001207}
1208
1209static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001210fd_specified(const char *function_name, int fd)
1211{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001212 if (fd == -1)
1213 return 0;
1214
1215 argument_unavailable_error(function_name, "fd");
1216 return 1;
1217}
1218
1219static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001220follow_symlinks_specified(const char *function_name, int follow_symlinks)
1221{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001222 if (follow_symlinks)
1223 return 0;
1224
1225 argument_unavailable_error(function_name, "follow_symlinks");
1226 return 1;
1227}
1228
1229static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001230path_and_dir_fd_invalid(const char *function_name, path_t *path, int dir_fd)
1231{
Steve Dowercc16be82016-09-08 10:35:16 -07001232 if (!path->wide && (dir_fd != DEFAULT_DIR_FD)
1233#ifndef MS_WINDOWS
1234 && !path->narrow
1235#endif
1236 ) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07001237 PyErr_Format(PyExc_ValueError,
1238 "%s: can't specify dir_fd without matching path",
1239 function_name);
1240 return 1;
1241 }
1242 return 0;
1243}
1244
1245static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001246dir_fd_and_fd_invalid(const char *function_name, int dir_fd, int fd)
1247{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001248 if ((dir_fd != DEFAULT_DIR_FD) && (fd != -1)) {
1249 PyErr_Format(PyExc_ValueError,
1250 "%s: can't specify both dir_fd and fd",
1251 function_name);
1252 return 1;
1253 }
1254 return 0;
1255}
1256
1257static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001258fd_and_follow_symlinks_invalid(const char *function_name, int fd,
1259 int follow_symlinks)
1260{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001261 if ((fd > 0) && (!follow_symlinks)) {
1262 PyErr_Format(PyExc_ValueError,
1263 "%s: cannot use fd and follow_symlinks together",
1264 function_name);
1265 return 1;
1266 }
1267 return 0;
1268}
1269
1270static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001271dir_fd_and_follow_symlinks_invalid(const char *function_name, int dir_fd,
1272 int follow_symlinks)
1273{
Larry Hastings9cf065c2012-06-22 16:30:09 -07001274 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
1275 PyErr_Format(PyExc_ValueError,
1276 "%s: cannot use dir_fd and follow_symlinks together",
1277 function_name);
1278 return 1;
1279 }
1280 return 0;
1281}
1282
Larry Hastings2f936352014-08-05 14:04:04 +10001283#ifdef MS_WINDOWS
Benjamin Petersonaf580df2016-09-06 10:46:49 -07001284 typedef long long Py_off_t;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001285#else
Larry Hastings2f936352014-08-05 14:04:04 +10001286 typedef off_t Py_off_t;
1287#endif
1288
1289static int
1290Py_off_t_converter(PyObject *arg, void *addr)
1291{
1292#ifdef HAVE_LARGEFILE_SUPPORT
1293 *((Py_off_t *)addr) = PyLong_AsLongLong(arg);
1294#else
1295 *((Py_off_t *)addr) = PyLong_AsLong(arg);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001296#endif
1297 if (PyErr_Occurred())
1298 return 0;
1299 return 1;
1300}
Larry Hastings2f936352014-08-05 14:04:04 +10001301
1302static PyObject *
1303PyLong_FromPy_off_t(Py_off_t offset)
1304{
1305#ifdef HAVE_LARGEFILE_SUPPORT
1306 return PyLong_FromLongLong(offset);
1307#else
1308 return PyLong_FromLong(offset);
Ross Lagerwallb1e5d592011-09-19 08:30:43 +02001309#endif
Larry Hastings2f936352014-08-05 14:04:04 +10001310}
1311
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001312#ifdef HAVE_SIGSET_T
1313/* Convert an iterable of integers to a sigset.
1314 Return 1 on success, return 0 and raise an exception on error. */
1315int
1316_Py_Sigset_Converter(PyObject *obj, void *addr)
1317{
1318 sigset_t *mask = (sigset_t *)addr;
1319 PyObject *iterator, *item;
1320 long signum;
1321 int overflow;
1322
Rémi Lapeyref0900192019-05-04 01:30:53 +02001323 // The extra parens suppress the unreachable-code warning with clang on MacOS
1324 if (sigemptyset(mask) < (0)) {
Serhiy Storchakad54cfb12018-05-08 07:48:50 +03001325 /* Probably only if mask == NULL. */
1326 PyErr_SetFromErrno(PyExc_OSError);
1327 return 0;
1328 }
1329
1330 iterator = PyObject_GetIter(obj);
1331 if (iterator == NULL) {
1332 return 0;
1333 }
1334
1335 while ((item = PyIter_Next(iterator)) != NULL) {
1336 signum = PyLong_AsLongAndOverflow(item, &overflow);
1337 Py_DECREF(item);
1338 if (signum <= 0 || signum >= NSIG) {
1339 if (overflow || signum != -1 || !PyErr_Occurred()) {
1340 PyErr_Format(PyExc_ValueError,
1341 "signal number %ld out of range", signum);
1342 }
1343 goto error;
1344 }
1345 if (sigaddset(mask, (int)signum)) {
1346 if (errno != EINVAL) {
1347 /* Probably impossible */
1348 PyErr_SetFromErrno(PyExc_OSError);
1349 goto error;
1350 }
1351 /* For backwards compatibility, allow idioms such as
1352 * `range(1, NSIG)` but warn about invalid signal numbers
1353 */
1354 const char msg[] =
1355 "invalid signal number %ld, please use valid_signals()";
1356 if (PyErr_WarnFormat(PyExc_RuntimeWarning, 1, msg, signum)) {
1357 goto error;
1358 }
1359 }
1360 }
1361 if (!PyErr_Occurred()) {
1362 Py_DECREF(iterator);
1363 return 1;
1364 }
1365
1366error:
1367 Py_DECREF(iterator);
1368 return 0;
1369}
1370#endif /* HAVE_SIGSET_T */
1371
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001372#ifdef MS_WINDOWS
Brian Curtinf5e76d02010-11-24 13:14:05 +00001373
1374static int
Brian Curtind25aef52011-06-13 15:16:04 -05001375win32_get_reparse_tag(HANDLE reparse_point_handle, ULONG *reparse_tag)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001376{
Martin Panter70214ad2016-08-04 02:38:59 +00001377 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
1378 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001379 DWORD n_bytes_returned;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001380
1381 if (0 == DeviceIoControl(
1382 reparse_point_handle,
1383 FSCTL_GET_REPARSE_POINT,
1384 NULL, 0, /* in buffer */
1385 target_buffer, sizeof(target_buffer),
1386 &n_bytes_returned,
1387 NULL)) /* we're not using OVERLAPPED_IO */
Brian Curtind25aef52011-06-13 15:16:04 -05001388 return FALSE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001389
1390 if (reparse_tag)
1391 *reparse_tag = rdb->ReparseTag;
1392
Brian Curtind25aef52011-06-13 15:16:04 -05001393 return TRUE;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001394}
Victor Stinner1ab6c2d2011-11-15 22:27:41 +01001395
Brian Curtinfc1be6d2010-11-24 13:23:18 +00001396#endif /* MS_WINDOWS */
Brian Curtinf5e76d02010-11-24 13:14:05 +00001397
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001398/* Return a dictionary corresponding to the POSIX environment table */
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001399#if defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
Jack Jansenea0c3822002-08-01 21:57:49 +00001400/* On Darwin/MacOSX a shared library or framework has no access to
Ronald Oussoren697e56d2013-01-25 17:57:13 +01001401** environ directly, we must obtain it with _NSGetEnviron(). See also
1402** man environ(7).
Jack Jansenea0c3822002-08-01 21:57:49 +00001403*/
1404#include <crt_externs.h>
pxinwrf2d7ac72019-05-21 18:46:37 +08001405#elif !defined(_MSC_VER) && (!defined(__WATCOMC__) || defined(__QNX__) || defined(__VXWORKS__))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001406extern char **environ;
Guido van Rossuma4916fa1996-05-23 22:58:55 +00001407#endif /* !_MSC_VER */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001408
Barry Warsaw53699e91996-12-10 23:23:01 +00001409static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001410convertenviron(void)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001411{
Victor Stinner8c62be82010-05-06 00:08:46 +00001412 PyObject *d;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001413#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00001414 wchar_t **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001415#else
Victor Stinner8c62be82010-05-06 00:08:46 +00001416 char **e;
Thomas Hellerf78f12a2007-11-08 19:33:05 +00001417#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +00001418
Victor Stinner8c62be82010-05-06 00:08:46 +00001419 d = PyDict_New();
1420 if (d == NULL)
1421 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001422#ifdef MS_WINDOWS
1423 /* _wenviron must be initialized in this way if the program is started
1424 through main() instead of wmain(). */
1425 _wgetenv(L"");
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001426 e = _wenviron;
Benoit Hudson723f71a2019-12-06 14:15:03 -05001427#elif defined(WITH_NEXT_FRAMEWORK) || (defined(__APPLE__) && defined(Py_ENABLE_SHARED))
1428 /* environ is not accessible as an extern in a shared object on OSX; use
1429 _NSGetEnviron to resolve it. The value changes if you add environment
1430 variables between calls to Py_Initialize, so don't cache the value. */
1431 e = *_NSGetEnviron();
Victor Stinner8c62be82010-05-06 00:08:46 +00001432#else
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001433 e = environ;
1434#endif
1435 if (e == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +00001436 return d;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001437 for (; *e != NULL; e++) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001438 PyObject *k;
1439 PyObject *v;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001440#ifdef MS_WINDOWS
1441 const wchar_t *p = wcschr(*e, L'=');
1442#else
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03001443 const char *p = strchr(*e, '=');
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001444#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001445 if (p == NULL)
1446 continue;
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001447#ifdef MS_WINDOWS
1448 k = PyUnicode_FromWideChar(*e, (Py_ssize_t)(p-*e));
1449#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001450 k = PyBytes_FromStringAndSize(*e, (int)(p-*e));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001451#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001452 if (k == NULL) {
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001453 Py_DECREF(d);
1454 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001455 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001456#ifdef MS_WINDOWS
1457 v = PyUnicode_FromWideChar(p+1, wcslen(p+1));
1458#else
Victor Stinner84ae1182010-05-06 22:05:07 +00001459 v = PyBytes_FromStringAndSize(p+1, strlen(p+1));
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001460#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001461 if (v == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00001462 Py_DECREF(k);
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001463 Py_DECREF(d);
1464 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00001465 }
Serhiy Storchaka6fef0f12018-12-10 12:10:56 +02001466 if (PyDict_GetItemWithError(d, k) == NULL) {
1467 if (PyErr_Occurred() || PyDict_SetItem(d, k, v) != 0) {
1468 Py_DECREF(v);
1469 Py_DECREF(k);
1470 Py_DECREF(d);
1471 return NULL;
1472 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001473 }
1474 Py_DECREF(k);
1475 Py_DECREF(v);
Guido van Rossumd48f2521997-12-05 22:19:34 +00001476 }
Victor Stinner8c62be82010-05-06 00:08:46 +00001477 return d;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001478}
1479
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001480/* Set a POSIX-specific error from errno, and return NULL */
1481
Barry Warsawd58d7641998-07-23 16:14:40 +00001482static PyObject *
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001483posix_error(void)
Guido van Rossumad0ee831995-03-01 10:34:45 +00001484{
Victor Stinner8c62be82010-05-06 00:08:46 +00001485 return PyErr_SetFromErrno(PyExc_OSError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001486}
Mark Hammondef8b6542001-05-13 08:04:26 +00001487
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00001488#ifdef MS_WINDOWS
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001489static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02001490win32_error(const char* function, const char* filename)
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001491{
Victor Stinner8c62be82010-05-06 00:08:46 +00001492 /* XXX We should pass the function name along in the future.
1493 (winreg.c also wants to pass the function name.)
1494 This would however require an additional param to the
1495 Windows error object, which is non-trivial.
1496 */
1497 errno = GetLastError();
1498 if (filename)
1499 return PyErr_SetFromWindowsErrWithFilename(errno, filename);
1500 else
1501 return PyErr_SetFromWindowsErr(errno);
Fredrik Lundhffb9c772000-07-09 14:49:51 +00001502}
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001503
Mark Hammondc2e85bd2002-10-03 05:10:39 +00001504static PyObject *
Steve Dower2438cdf2019-03-29 16:37:16 -07001505win32_error_object_err(const char* function, PyObject* filename, DWORD err)
Victor Stinnereb5657a2011-09-30 01:44:27 +02001506{
1507 /* XXX - see win32_error for comments on 'function' */
Victor Stinnereb5657a2011-09-30 01:44:27 +02001508 if (filename)
1509 return PyErr_SetExcFromWindowsErrWithFilenameObject(
Andrew Svetlov2606a6f2012-12-19 14:33:35 +02001510 PyExc_OSError,
Steve Dower2438cdf2019-03-29 16:37:16 -07001511 err,
Victor Stinnereb5657a2011-09-30 01:44:27 +02001512 filename);
1513 else
Steve Dower2438cdf2019-03-29 16:37:16 -07001514 return PyErr_SetFromWindowsErr(err);
1515}
1516
1517static PyObject *
1518win32_error_object(const char* function, PyObject* filename)
1519{
1520 errno = GetLastError();
1521 return win32_error_object_err(function, filename, errno);
Victor Stinnereb5657a2011-09-30 01:44:27 +02001522}
1523
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001524#endif /* MS_WINDOWS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001525
Larry Hastings9cf065c2012-06-22 16:30:09 -07001526static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001527posix_path_object_error(PyObject *path)
1528{
1529 return PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path);
1530}
1531
1532static PyObject *
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001533path_object_error(PyObject *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07001534{
1535#ifdef MS_WINDOWS
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001536 return PyErr_SetExcFromWindowsErrWithFilenameObject(
1537 PyExc_OSError, 0, path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001538#else
Alexey Izbyshev83460312018-10-20 03:28:22 +03001539 return posix_path_object_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07001540#endif
1541}
1542
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001543static PyObject *
1544path_object_error2(PyObject *path, PyObject *path2)
1545{
1546#ifdef MS_WINDOWS
1547 return PyErr_SetExcFromWindowsErrWithFilenameObjects(
1548 PyExc_OSError, 0, path, path2);
1549#else
1550 return PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, path, path2);
1551#endif
1552}
1553
1554static PyObject *
1555path_error(path_t *path)
1556{
1557 return path_object_error(path->object);
1558}
Larry Hastings31826802013-10-19 00:09:25 -07001559
Larry Hastingsb0827312014-02-09 22:05:19 -08001560static PyObject *
Alexey Izbyshev83460312018-10-20 03:28:22 +03001561posix_path_error(path_t *path)
1562{
1563 return posix_path_object_error(path->object);
1564}
1565
1566static PyObject *
Larry Hastingsb0827312014-02-09 22:05:19 -08001567path_error2(path_t *path, path_t *path2)
1568{
Serhiy Storchaka2674bc72016-10-08 20:16:57 +03001569 return path_object_error2(path->object, path2->object);
Larry Hastingsb0827312014-02-09 22:05:19 -08001570}
1571
1572
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001573/* POSIX generic methods */
1574
Larry Hastings2f936352014-08-05 14:04:04 +10001575static int
1576fildes_converter(PyObject *o, void *p)
Fred Drake4d1e64b2002-04-15 19:40:07 +00001577{
Victor Stinner8c62be82010-05-06 00:08:46 +00001578 int fd;
Larry Hastings2f936352014-08-05 14:04:04 +10001579 int *pointer = (int *)p;
1580 fd = PyObject_AsFileDescriptor(o);
Victor Stinner8c62be82010-05-06 00:08:46 +00001581 if (fd < 0)
Larry Hastings2f936352014-08-05 14:04:04 +10001582 return 0;
Larry Hastings2f936352014-08-05 14:04:04 +10001583 *pointer = fd;
1584 return 1;
1585}
1586
1587static PyObject *
1588posix_fildes_fd(int fd, int (*func)(int))
1589{
1590 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001591 int async_err = 0;
1592
1593 do {
1594 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04001595 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001596 res = (*func)(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04001597 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00001598 Py_END_ALLOW_THREADS
1599 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
1600 if (res != 0)
1601 return (!async_err) ? posix_error() : NULL;
1602 Py_RETURN_NONE;
Fred Drake4d1e64b2002-04-15 19:40:07 +00001603}
Guido van Rossum21142a01999-01-08 21:05:37 +00001604
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001605
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00001606#ifdef MS_WINDOWS
Thomas Wouters477c8d52006-05-27 19:21:47 +00001607/* This is a reimplementation of the C library's chdir function,
1608 but one that produces Win32 errors instead of DOS error codes.
1609 chdir is essentially a wrapper around SetCurrentDirectory; however,
1610 it also needs to set "magic" environment variables indicating
1611 the per-drive current directory, which are of the form =<drive>: */
Benjamin Peterson206e3072008-10-19 14:07:49 +00001612static BOOL __stdcall
Thomas Wouters477c8d52006-05-27 19:21:47 +00001613win32_wchdir(LPCWSTR path)
1614{
Victor Stinnered537822015-12-13 21:40:26 +01001615 wchar_t path_buf[MAX_PATH], *new_path = path_buf;
Victor Stinner8c62be82010-05-06 00:08:46 +00001616 int result;
1617 wchar_t env[4] = L"=x:";
Thomas Wouters477c8d52006-05-27 19:21:47 +00001618
Victor Stinner8c62be82010-05-06 00:08:46 +00001619 if(!SetCurrentDirectoryW(path))
1620 return FALSE;
Victor Stinnered537822015-12-13 21:40:26 +01001621 result = GetCurrentDirectoryW(Py_ARRAY_LENGTH(path_buf), new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001622 if (!result)
1623 return FALSE;
Victor Stinnere847d712015-12-14 00:21:50 +01001624 if (result > Py_ARRAY_LENGTH(path_buf)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001625 new_path = PyMem_RawMalloc(result * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00001626 if (!new_path) {
1627 SetLastError(ERROR_OUTOFMEMORY);
1628 return FALSE;
1629 }
1630 result = GetCurrentDirectoryW(result, new_path);
1631 if (!result) {
Victor Stinnerb6404912013-07-07 16:21:41 +02001632 PyMem_RawFree(new_path);
Victor Stinner8c62be82010-05-06 00:08:46 +00001633 return FALSE;
1634 }
1635 }
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001636 int is_unc_like_path = (wcsncmp(new_path, L"\\\\", 2) == 0 ||
1637 wcsncmp(new_path, L"//", 2) == 0);
1638 if (!is_unc_like_path) {
1639 env[1] = new_path[0];
1640 result = SetEnvironmentVariableW(env, new_path);
1641 }
Victor Stinnered537822015-12-13 21:40:26 +01001642 if (new_path != path_buf)
Victor Stinnerb6404912013-07-07 16:21:41 +02001643 PyMem_RawFree(new_path);
Alexey Izbyshev3e197c72018-03-01 12:13:56 +03001644 return result ? TRUE : FALSE;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001645}
1646#endif
1647
Martin v. Löwis14694662006-02-03 12:54:16 +00001648#ifdef MS_WINDOWS
1649/* The CRT of Windows has a number of flaws wrt. its stat() implementation:
1650 - time stamps are restricted to second resolution
1651 - file modification times suffer from forth-and-back conversions between
1652 UTC and local time
1653 Therefore, we implement our own stat, based on the Win32 API directly.
1654*/
Victor Stinner8c62be82010-05-06 00:08:46 +00001655#define HAVE_STAT_NSEC 1
Zachary Ware63f277b2014-06-19 09:46:37 -05001656#define HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES 1
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001657#define HAVE_STRUCT_STAT_ST_REPARSE_TAG 1
Martin v. Löwis14694662006-02-03 12:54:16 +00001658
Victor Stinner6036e442015-03-08 01:58:04 +01001659static void
Steve Dowercc16be82016-09-08 10:35:16 -07001660find_data_to_file_info(WIN32_FIND_DATAW *pFileData,
1661 BY_HANDLE_FILE_INFORMATION *info,
1662 ULONG *reparse_tag)
Victor Stinner6036e442015-03-08 01:58:04 +01001663{
1664 memset(info, 0, sizeof(*info));
1665 info->dwFileAttributes = pFileData->dwFileAttributes;
1666 info->ftCreationTime = pFileData->ftCreationTime;
1667 info->ftLastAccessTime = pFileData->ftLastAccessTime;
1668 info->ftLastWriteTime = pFileData->ftLastWriteTime;
1669 info->nFileSizeHigh = pFileData->nFileSizeHigh;
1670 info->nFileSizeLow = pFileData->nFileSizeLow;
1671/* info->nNumberOfLinks = 1; */
1672 if (pFileData->dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)
1673 *reparse_tag = pFileData->dwReserved0;
1674 else
1675 *reparse_tag = 0;
1676}
1677
Guido van Rossumd8faa362007-04-27 19:54:29 +00001678static BOOL
Steve Dowercc16be82016-09-08 10:35:16 -07001679attributes_from_dir(LPCWSTR pszFile, BY_HANDLE_FILE_INFORMATION *info, ULONG *reparse_tag)
Guido van Rossumd8faa362007-04-27 19:54:29 +00001680{
Victor Stinner8c62be82010-05-06 00:08:46 +00001681 HANDLE hFindFile;
1682 WIN32_FIND_DATAW FileData;
1683 hFindFile = FindFirstFileW(pszFile, &FileData);
1684 if (hFindFile == INVALID_HANDLE_VALUE)
1685 return FALSE;
1686 FindClose(hFindFile);
Steve Dowercc16be82016-09-08 10:35:16 -07001687 find_data_to_file_info(&FileData, info, reparse_tag);
Victor Stinner8c62be82010-05-06 00:08:46 +00001688 return TRUE;
Guido van Rossumd8faa362007-04-27 19:54:29 +00001689}
1690
Brian Curtind25aef52011-06-13 15:16:04 -05001691static int
Steve Dowercc16be82016-09-08 10:35:16 -07001692win32_xstat_impl(const wchar_t *path, struct _Py_stat_struct *result,
Brian Curtind25aef52011-06-13 15:16:04 -05001693 BOOL traverse)
1694{
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001695 HANDLE hFile;
1696 BY_HANDLE_FILE_INFORMATION fileInfo;
1697 FILE_ATTRIBUTE_TAG_INFO tagInfo = { 0 };
1698 DWORD fileType, error;
1699 BOOL isUnhandledTag = FALSE;
1700 int retval = 0;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001701
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001702 DWORD access = FILE_READ_ATTRIBUTES;
1703 DWORD flags = FILE_FLAG_BACKUP_SEMANTICS; /* Allow opening directories. */
1704 if (!traverse) {
1705 flags |= FILE_FLAG_OPEN_REPARSE_POINT;
1706 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001707
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001708 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING, flags, NULL);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001709 if (hFile == INVALID_HANDLE_VALUE) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001710 /* Either the path doesn't exist, or the caller lacks access. */
1711 error = GetLastError();
1712 switch (error) {
1713 case ERROR_ACCESS_DENIED: /* Cannot sync or read attributes. */
1714 case ERROR_SHARING_VIOLATION: /* It's a paging file. */
1715 /* Try reading the parent directory. */
1716 if (!attributes_from_dir(path, &fileInfo, &tagInfo.ReparseTag)) {
1717 /* Cannot read the parent directory. */
1718 SetLastError(error);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001719 return -1;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001720 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001721 if (fileInfo.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1722 if (traverse ||
1723 !IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1724 /* The stat call has to traverse but cannot, so fail. */
1725 SetLastError(error);
Brian Curtind25aef52011-06-13 15:16:04 -05001726 return -1;
Mark Becwarb82bfac2019-02-02 16:08:23 -05001727 }
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001728 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001729 break;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001730
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001731 case ERROR_INVALID_PARAMETER:
1732 /* \\.\con requires read or write access. */
1733 hFile = CreateFileW(path, access | GENERIC_READ,
1734 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
1735 OPEN_EXISTING, flags, NULL);
1736 if (hFile == INVALID_HANDLE_VALUE) {
1737 SetLastError(error);
1738 return -1;
1739 }
1740 break;
1741
1742 case ERROR_CANT_ACCESS_FILE:
1743 /* bpo37834: open unhandled reparse points if traverse fails. */
1744 if (traverse) {
1745 traverse = FALSE;
1746 isUnhandledTag = TRUE;
1747 hFile = CreateFileW(path, access, 0, NULL, OPEN_EXISTING,
1748 flags | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
1749 }
1750 if (hFile == INVALID_HANDLE_VALUE) {
1751 SetLastError(error);
1752 return -1;
1753 }
1754 break;
1755
1756 default:
1757 return -1;
1758 }
Brian Curtinf5e76d02010-11-24 13:14:05 +00001759 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001760
1761 if (hFile != INVALID_HANDLE_VALUE) {
1762 /* Handle types other than files on disk. */
1763 fileType = GetFileType(hFile);
1764 if (fileType != FILE_TYPE_DISK) {
1765 if (fileType == FILE_TYPE_UNKNOWN && GetLastError() != 0) {
1766 retval = -1;
1767 goto cleanup;
1768 }
1769 DWORD fileAttributes = GetFileAttributesW(path);
1770 memset(result, 0, sizeof(*result));
1771 if (fileAttributes != INVALID_FILE_ATTRIBUTES &&
1772 fileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
1773 /* \\.\pipe\ or \\.\mailslot\ */
1774 result->st_mode = _S_IFDIR;
1775 } else if (fileType == FILE_TYPE_CHAR) {
1776 /* \\.\nul */
1777 result->st_mode = _S_IFCHR;
1778 } else if (fileType == FILE_TYPE_PIPE) {
1779 /* \\.\pipe\spam */
1780 result->st_mode = _S_IFIFO;
1781 }
1782 /* FILE_TYPE_UNKNOWN, e.g. \\.\mailslot\waitfor.exe\spam */
1783 goto cleanup;
1784 }
1785
1786 /* Query the reparse tag, and traverse a non-link. */
1787 if (!traverse) {
1788 if (!GetFileInformationByHandleEx(hFile, FileAttributeTagInfo,
1789 &tagInfo, sizeof(tagInfo))) {
1790 /* Allow devices that do not support FileAttributeTagInfo. */
1791 switch (GetLastError()) {
1792 case ERROR_INVALID_PARAMETER:
1793 case ERROR_INVALID_FUNCTION:
1794 case ERROR_NOT_SUPPORTED:
1795 tagInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL;
1796 tagInfo.ReparseTag = 0;
1797 break;
1798 default:
1799 retval = -1;
1800 goto cleanup;
1801 }
1802 } else if (tagInfo.FileAttributes &
1803 FILE_ATTRIBUTE_REPARSE_POINT) {
1804 if (IsReparseTagNameSurrogate(tagInfo.ReparseTag)) {
1805 if (isUnhandledTag) {
1806 /* Traversing previously failed for either this link
1807 or its target. */
1808 SetLastError(ERROR_CANT_ACCESS_FILE);
1809 retval = -1;
1810 goto cleanup;
1811 }
1812 /* Traverse a non-link, but not if traversing already failed
1813 for an unhandled tag. */
1814 } else if (!isUnhandledTag) {
1815 CloseHandle(hFile);
1816 return win32_xstat_impl(path, result, TRUE);
1817 }
1818 }
1819 }
1820
1821 if (!GetFileInformationByHandle(hFile, &fileInfo)) {
1822 switch (GetLastError()) {
1823 case ERROR_INVALID_PARAMETER:
1824 case ERROR_INVALID_FUNCTION:
1825 case ERROR_NOT_SUPPORTED:
Steve Dower772ec0f2019-09-04 14:42:54 -07001826 /* Volumes and physical disks are block devices, e.g.
1827 \\.\C: and \\.\PhysicalDrive0. */
1828 memset(result, 0, sizeof(*result));
1829 result->st_mode = 0x6000; /* S_IFBLK */
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001830 goto cleanup;
1831 }
Steve Dower772ec0f2019-09-04 14:42:54 -07001832 retval = -1;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001833 goto cleanup;
1834 }
1835 }
1836
1837 _Py_attribute_data_to_stat(&fileInfo, tagInfo.ReparseTag, result);
1838
1839 if (!(fileInfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
1840 /* Fix the file execute permissions. This hack sets S_IEXEC if
1841 the filename has an extension that is commonly used by files
1842 that CreateProcessW can execute. A real implementation calls
1843 GetSecurityInfo, OpenThreadToken/OpenProcessToken, and
1844 AccessCheck to check for generic read, write, and execute
1845 access. */
1846 const wchar_t *fileExtension = wcsrchr(path, '.');
1847 if (fileExtension) {
1848 if (_wcsicmp(fileExtension, L".exe") == 0 ||
1849 _wcsicmp(fileExtension, L".bat") == 0 ||
1850 _wcsicmp(fileExtension, L".cmd") == 0 ||
1851 _wcsicmp(fileExtension, L".com") == 0) {
1852 result->st_mode |= 0111;
1853 }
1854 }
1855 }
1856
1857cleanup:
1858 if (hFile != INVALID_HANDLE_VALUE) {
Steve Dower772ec0f2019-09-04 14:42:54 -07001859 /* Preserve last error if we are failing */
1860 error = retval ? GetLastError() : 0;
1861 if (!CloseHandle(hFile)) {
1862 retval = -1;
1863 } else if (retval) {
1864 /* Restore last error */
1865 SetLastError(error);
1866 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001867 }
1868
1869 return retval;
Brian Curtinf5e76d02010-11-24 13:14:05 +00001870}
1871
1872static int
Steve Dowercc16be82016-09-08 10:35:16 -07001873win32_xstat(const wchar_t *path, struct _Py_stat_struct *result, BOOL traverse)
Brian Curtinf5e76d02010-11-24 13:14:05 +00001874{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001875 /* Protocol violation: we explicitly clear errno, instead of
1876 setting it to a POSIX error. Callers should use GetLastError. */
Brian Curtind25aef52011-06-13 15:16:04 -05001877 int code = win32_xstat_impl(path, result, traverse);
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001878 errno = 0;
1879 return code;
1880}
Brian Curtind25aef52011-06-13 15:16:04 -05001881/* About the following functions: win32_lstat_w, win32_stat, win32_stat_w
Brian Curtind40e6f72010-07-08 21:39:08 +00001882
1883 In Posix, stat automatically traverses symlinks and returns the stat
1884 structure for the target. In Windows, the equivalent GetFileAttributes by
1885 default does not traverse symlinks and instead returns attributes for
1886 the symlink.
1887
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001888 Instead, we will open the file (which *does* traverse symlinks by default)
1889 and GetFileInformationByHandle(). */
Brian Curtind40e6f72010-07-08 21:39:08 +00001890
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00001891static int
Steve Dowercc16be82016-09-08 10:35:16 -07001892win32_lstat(const wchar_t* path, struct _Py_stat_struct *result)
Martin v. Löwis14694662006-02-03 12:54:16 +00001893{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001894 return win32_xstat(path, result, FALSE);
Martin v. Löwis14694662006-02-03 12:54:16 +00001895}
1896
Victor Stinner8c62be82010-05-06 00:08:46 +00001897static int
Steve Dowercc16be82016-09-08 10:35:16 -07001898win32_stat(const wchar_t* path, struct _Py_stat_struct *result)
Brian Curtind40e6f72010-07-08 21:39:08 +00001899{
Hirokazu Yamamoto427d3142010-12-04 10:16:05 +00001900 return win32_xstat(path, result, TRUE);
Brian Curtind40e6f72010-07-08 21:39:08 +00001901}
1902
Martin v. Löwis14694662006-02-03 12:54:16 +00001903#endif /* MS_WINDOWS */
1904
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001905PyDoc_STRVAR(stat_result__doc__,
Larry Hastings9cf065c2012-06-22 16:30:09 -07001906"stat_result: Result from stat, fstat, or lstat.\n\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001907This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00001908 (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001909or via the attributes st_mode, st_ino, st_dev, st_nlink, st_uid, and so on.\n\
1910\n\
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001911Posix/windows: If your platform supports st_blksize, st_blocks, st_rdev,\n\
1912or st_flags, they are available as attributes only.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001913\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001914See os.stat for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001915
1916static PyStructSequence_Field stat_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00001917 {"st_mode", "protection bits"},
1918 {"st_ino", "inode"},
1919 {"st_dev", "device"},
1920 {"st_nlink", "number of hard links"},
1921 {"st_uid", "user ID of owner"},
1922 {"st_gid", "group ID of owner"},
1923 {"st_size", "total size, in bytes"},
1924 /* The NULL is replaced with PyStructSequence_UnnamedField later. */
1925 {NULL, "integer time of last access"},
1926 {NULL, "integer time of last modification"},
1927 {NULL, "integer time of last change"},
1928 {"st_atime", "time of last access"},
1929 {"st_mtime", "time of last modification"},
1930 {"st_ctime", "time of last change"},
Larry Hastings6fe20b32012-04-19 15:07:49 -07001931 {"st_atime_ns", "time of last access in nanoseconds"},
1932 {"st_mtime_ns", "time of last modification in nanoseconds"},
1933 {"st_ctime_ns", "time of last change in nanoseconds"},
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001934#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00001935 {"st_blksize", "blocksize for filesystem I/O"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001936#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001937#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00001938 {"st_blocks", "number of blocks allocated"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001939#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001940#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00001941 {"st_rdev", "device type (if inode device)"},
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001942#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001943#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00001944 {"st_flags", "user defined flags for file"},
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001945#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001946#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00001947 {"st_gen", "generation number"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001948#endif
1949#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00001950 {"st_birthtime", "time of creation"},
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001951#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05001952#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
1953 {"st_file_attributes", "Windows file attribute bits"},
1954#endif
jcea6c51d512018-01-28 14:00:08 +01001955#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
1956 {"st_fstype", "Type of filesystem"},
1957#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07001958#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
1959 {"st_reparse_tag", "Windows reparse tag"},
1960#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00001961 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001962};
1963
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001964#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Larry Hastings6fe20b32012-04-19 15:07:49 -07001965#define ST_BLKSIZE_IDX 16
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001966#else
Larry Hastings6fe20b32012-04-19 15:07:49 -07001967#define ST_BLKSIZE_IDX 15
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001968#endif
1969
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001970#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001971#define ST_BLOCKS_IDX (ST_BLKSIZE_IDX+1)
1972#else
1973#define ST_BLOCKS_IDX ST_BLKSIZE_IDX
1974#endif
1975
Martin v. Löwis60a5d722002-10-16 20:28:25 +00001976#ifdef HAVE_STRUCT_STAT_ST_RDEV
Guido van Rossum98bf58f2001-10-18 20:34:25 +00001977#define ST_RDEV_IDX (ST_BLOCKS_IDX+1)
1978#else
1979#define ST_RDEV_IDX ST_BLOCKS_IDX
1980#endif
1981
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00001982#ifdef HAVE_STRUCT_STAT_ST_FLAGS
1983#define ST_FLAGS_IDX (ST_RDEV_IDX+1)
1984#else
1985#define ST_FLAGS_IDX ST_RDEV_IDX
1986#endif
1987
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001988#ifdef HAVE_STRUCT_STAT_ST_GEN
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001989#define ST_GEN_IDX (ST_FLAGS_IDX+1)
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001990#else
Martin v. Löwisf09582e2005-08-14 21:42:34 +00001991#define ST_GEN_IDX ST_FLAGS_IDX
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00001992#endif
1993
1994#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
1995#define ST_BIRTHTIME_IDX (ST_GEN_IDX+1)
1996#else
1997#define ST_BIRTHTIME_IDX ST_GEN_IDX
1998#endif
1999
Zachary Ware63f277b2014-06-19 09:46:37 -05002000#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2001#define ST_FILE_ATTRIBUTES_IDX (ST_BIRTHTIME_IDX+1)
2002#else
2003#define ST_FILE_ATTRIBUTES_IDX ST_BIRTHTIME_IDX
2004#endif
2005
jcea6c51d512018-01-28 14:00:08 +01002006#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2007#define ST_FSTYPE_IDX (ST_FILE_ATTRIBUTES_IDX+1)
2008#else
2009#define ST_FSTYPE_IDX ST_FILE_ATTRIBUTES_IDX
2010#endif
2011
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002012#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2013#define ST_REPARSE_TAG_IDX (ST_FSTYPE_IDX+1)
2014#else
2015#define ST_REPARSE_TAG_IDX ST_FSTYPE_IDX
2016#endif
2017
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002018static PyStructSequence_Desc stat_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002019 "stat_result", /* name */
2020 stat_result__doc__, /* doc */
2021 stat_result_fields,
2022 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002023};
2024
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002025PyDoc_STRVAR(statvfs_result__doc__,
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002026"statvfs_result: Result from statvfs or fstatvfs.\n\n\
2027This object may be accessed either as a tuple of\n\
Fred Drakef7ce04d2002-06-20 18:31:21 +00002028 (bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax),\n\
Guido van Rossuma4dc73e2001-10-18 20:53:15 +00002029or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on.\n\
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002030\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00002031See os.statvfs for more information.");
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002032
2033static PyStructSequence_Field statvfs_result_fields[] = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002034 {"f_bsize", },
2035 {"f_frsize", },
2036 {"f_blocks", },
2037 {"f_bfree", },
2038 {"f_bavail", },
2039 {"f_files", },
2040 {"f_ffree", },
2041 {"f_favail", },
2042 {"f_flag", },
2043 {"f_namemax",},
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +01002044 {"f_fsid", },
Victor Stinner8c62be82010-05-06 00:08:46 +00002045 {0}
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002046};
2047
2048static PyStructSequence_Desc statvfs_result_desc = {
Victor Stinner8c62be82010-05-06 00:08:46 +00002049 "statvfs_result", /* name */
2050 statvfs_result__doc__, /* doc */
2051 statvfs_result_fields,
2052 10
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002053};
2054
Ross Lagerwall7807c352011-03-17 20:20:30 +02002055#if defined(HAVE_WAITID) && !defined(__APPLE__)
2056PyDoc_STRVAR(waitid_result__doc__,
2057"waitid_result: Result from waitid.\n\n\
2058This object may be accessed either as a tuple of\n\
2059 (si_pid, si_uid, si_signo, si_status, si_code),\n\
2060or via the attributes si_pid, si_uid, and so on.\n\
2061\n\
2062See os.waitid for more information.");
2063
2064static PyStructSequence_Field waitid_result_fields[] = {
2065 {"si_pid", },
2066 {"si_uid", },
2067 {"si_signo", },
2068 {"si_status", },
2069 {"si_code", },
2070 {0}
2071};
2072
2073static PyStructSequence_Desc waitid_result_desc = {
2074 "waitid_result", /* name */
2075 waitid_result__doc__, /* doc */
2076 waitid_result_fields,
2077 5
2078};
Benjamin Petersonbad9c2f2011-08-02 18:42:14 -05002079#endif
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002080static newfunc structseq_new;
2081
2082static PyObject *
2083statresult_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2084{
Victor Stinner8c62be82010-05-06 00:08:46 +00002085 PyStructSequence *result;
2086 int i;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002087
Victor Stinner8c62be82010-05-06 00:08:46 +00002088 result = (PyStructSequence*)structseq_new(type, args, kwds);
2089 if (!result)
2090 return NULL;
2091 /* If we have been initialized from a tuple,
2092 st_?time might be set to None. Initialize it
2093 from the int slots. */
2094 for (i = 7; i <= 9; i++) {
2095 if (result->ob_item[i+3] == Py_None) {
2096 Py_DECREF(Py_None);
2097 Py_INCREF(result->ob_item[i]);
2098 result->ob_item[i+3] = result->ob_item[i];
2099 }
2100 }
2101 return (PyObject*)result;
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002102}
2103
Eddie Elizondob3966632019-11-05 07:16:14 -08002104static int
2105_posix_clear(PyObject *module)
2106{
Hai Shif707d942020-03-16 21:15:01 +08002107 Py_CLEAR(get_posix_state(module)->billion);
2108 Py_CLEAR(get_posix_state(module)->DirEntryType);
2109 Py_CLEAR(get_posix_state(module)->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002110#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Hai Shif707d942020-03-16 21:15:01 +08002111 Py_CLEAR(get_posix_state(module)->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002112#endif
Hai Shif707d942020-03-16 21:15:01 +08002113 Py_CLEAR(get_posix_state(module)->StatResultType);
2114 Py_CLEAR(get_posix_state(module)->StatVFSResultType);
2115 Py_CLEAR(get_posix_state(module)->TerminalSizeType);
2116 Py_CLEAR(get_posix_state(module)->TimesResultType);
2117 Py_CLEAR(get_posix_state(module)->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002118#if defined(HAVE_WAITID) && !defined(__APPLE__)
Hai Shif707d942020-03-16 21:15:01 +08002119 Py_CLEAR(get_posix_state(module)->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002120#endif
2121#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +08002122 Py_CLEAR(get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002123#endif
Hai Shif707d942020-03-16 21:15:01 +08002124 Py_CLEAR(get_posix_state(module)->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002125 return 0;
2126}
Martin v. Löwisf607bda2002-10-16 18:27:39 +00002127
Eddie Elizondob3966632019-11-05 07:16:14 -08002128static int
2129_posix_traverse(PyObject *module, visitproc visit, void *arg)
2130{
Hai Shif707d942020-03-16 21:15:01 +08002131 Py_VISIT(get_posix_state(module)->billion);
2132 Py_VISIT(get_posix_state(module)->DirEntryType);
2133 Py_VISIT(get_posix_state(module)->ScandirIteratorType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002134#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Hai Shif707d942020-03-16 21:15:01 +08002135 Py_VISIT(get_posix_state(module)->SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002136#endif
Hai Shif707d942020-03-16 21:15:01 +08002137 Py_VISIT(get_posix_state(module)->StatResultType);
2138 Py_VISIT(get_posix_state(module)->StatVFSResultType);
2139 Py_VISIT(get_posix_state(module)->TerminalSizeType);
2140 Py_VISIT(get_posix_state(module)->TimesResultType);
2141 Py_VISIT(get_posix_state(module)->UnameResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002142#if defined(HAVE_WAITID) && !defined(__APPLE__)
Hai Shif707d942020-03-16 21:15:01 +08002143 Py_VISIT(get_posix_state(module)->WaitidResultType);
Eddie Elizondob3966632019-11-05 07:16:14 -08002144#endif
2145#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +08002146 Py_VISIT(get_posix_state(module)->struct_rusage);
Eddie Elizondob3966632019-11-05 07:16:14 -08002147#endif
Hai Shif707d942020-03-16 21:15:01 +08002148 Py_VISIT(get_posix_state(module)->st_mode);
Eddie Elizondob3966632019-11-05 07:16:14 -08002149 return 0;
2150}
2151
2152static void
2153_posix_free(void *module)
2154{
2155 _posix_clear((PyObject *)module);
2156}
Larry Hastings6fe20b32012-04-19 15:07:49 -07002157
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002158static void
Victor Stinner4195b5c2012-02-08 23:03:19 +01002159fill_time(PyObject *v, int index, time_t sec, unsigned long nsec)
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002160{
Larry Hastings6fe20b32012-04-19 15:07:49 -07002161 PyObject *s = _PyLong_FromTime_t(sec);
2162 PyObject *ns_fractional = PyLong_FromUnsignedLong(nsec);
2163 PyObject *s_in_ns = NULL;
2164 PyObject *ns_total = NULL;
2165 PyObject *float_s = NULL;
2166
2167 if (!(s && ns_fractional))
2168 goto exit;
2169
Eddie Elizondob3966632019-11-05 07:16:14 -08002170 s_in_ns = PyNumber_Multiply(s, _posixstate_global->billion);
Larry Hastings6fe20b32012-04-19 15:07:49 -07002171 if (!s_in_ns)
2172 goto exit;
2173
2174 ns_total = PyNumber_Add(s_in_ns, ns_fractional);
2175 if (!ns_total)
2176 goto exit;
2177
Victor Stinner01b5aab2017-10-24 02:02:00 -07002178 float_s = PyFloat_FromDouble(sec + 1e-9*nsec);
2179 if (!float_s) {
2180 goto exit;
Larry Hastings6fe20b32012-04-19 15:07:49 -07002181 }
2182
2183 PyStructSequence_SET_ITEM(v, index, s);
2184 PyStructSequence_SET_ITEM(v, index+3, float_s);
2185 PyStructSequence_SET_ITEM(v, index+6, ns_total);
2186 s = NULL;
2187 float_s = NULL;
2188 ns_total = NULL;
2189exit:
2190 Py_XDECREF(s);
2191 Py_XDECREF(ns_fractional);
2192 Py_XDECREF(s_in_ns);
2193 Py_XDECREF(ns_total);
2194 Py_XDECREF(float_s);
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002195}
2196
Tim Peters5aa91602002-01-30 05:46:57 +00002197/* pack a system stat C structure into the Python stat tuple
Fred Drake699f3522000-06-29 21:12:41 +00002198 (used by posix_stat() and posix_fstat()) */
2199static PyObject*
Victor Stinner4195b5c2012-02-08 23:03:19 +01002200_pystat_fromstructstat(STRUCT_STAT *st)
Fred Drake699f3522000-06-29 21:12:41 +00002201{
Victor Stinner8c62be82010-05-06 00:08:46 +00002202 unsigned long ansec, mnsec, cnsec;
Eddie Elizondob3966632019-11-05 07:16:14 -08002203 PyObject *StatResultType = _posixstate_global->StatResultType;
2204 PyObject *v = PyStructSequence_New((PyTypeObject *)StatResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +00002205 if (v == NULL)
2206 return NULL;
Fred Drake699f3522000-06-29 21:12:41 +00002207
Victor Stinner8c62be82010-05-06 00:08:46 +00002208 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long)st->st_mode));
Victor Stinner0f6d7332017-03-09 17:34:28 +01002209 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(st->st_ino));
xdegaye50e86032017-05-22 11:15:08 +02002210 PyStructSequence_SET_ITEM(v, 1, PyLong_FromUnsignedLongLong(st->st_ino));
Serhiy Storchaka404fa922013-01-02 18:22:23 +02002211#ifdef MS_WINDOWS
2212 PyStructSequence_SET_ITEM(v, 2, PyLong_FromUnsignedLong(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002213#else
Serhiy Storchakab2653b32015-01-18 11:12:11 +02002214 PyStructSequence_SET_ITEM(v, 2, _PyLong_FromDev(st->st_dev));
Fred Drake699f3522000-06-29 21:12:41 +00002215#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002216 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long)st->st_nlink));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02002217#if defined(MS_WINDOWS)
2218 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong(0));
2219 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong(0));
2220#else
2221 PyStructSequence_SET_ITEM(v, 4, _PyLong_FromUid(st->st_uid));
2222 PyStructSequence_SET_ITEM(v, 5, _PyLong_FromGid(st->st_gid));
2223#endif
xdegaye50e86032017-05-22 11:15:08 +02002224 Py_BUILD_ASSERT(sizeof(long long) >= sizeof(st->st_size));
2225 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLongLong(st->st_size));
Martin v. Löwis94717ed2002-09-09 14:24:16 +00002226
Martin v. Löwis14694662006-02-03 12:54:16 +00002227#if defined(HAVE_STAT_TV_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002228 ansec = st->st_atim.tv_nsec;
2229 mnsec = st->st_mtim.tv_nsec;
2230 cnsec = st->st_ctim.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002231#elif defined(HAVE_STAT_TV_NSEC2)
Victor Stinner8c62be82010-05-06 00:08:46 +00002232 ansec = st->st_atimespec.tv_nsec;
2233 mnsec = st->st_mtimespec.tv_nsec;
2234 cnsec = st->st_ctimespec.tv_nsec;
Martin v. Löwis14694662006-02-03 12:54:16 +00002235#elif defined(HAVE_STAT_NSEC)
Victor Stinner8c62be82010-05-06 00:08:46 +00002236 ansec = st->st_atime_nsec;
2237 mnsec = st->st_mtime_nsec;
2238 cnsec = st->st_ctime_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002239#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002240 ansec = mnsec = cnsec = 0;
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002241#endif
Victor Stinner4195b5c2012-02-08 23:03:19 +01002242 fill_time(v, 7, st->st_atime, ansec);
2243 fill_time(v, 8, st->st_mtime, mnsec);
2244 fill_time(v, 9, st->st_ctime, cnsec);
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002245
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002246#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +00002247 PyStructSequence_SET_ITEM(v, ST_BLKSIZE_IDX,
2248 PyLong_FromLong((long)st->st_blksize));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002249#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002250#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
Victor Stinner8c62be82010-05-06 00:08:46 +00002251 PyStructSequence_SET_ITEM(v, ST_BLOCKS_IDX,
2252 PyLong_FromLong((long)st->st_blocks));
Guido van Rossum98bf58f2001-10-18 20:34:25 +00002253#endif
Martin v. Löwis60a5d722002-10-16 20:28:25 +00002254#ifdef HAVE_STRUCT_STAT_ST_RDEV
Victor Stinner8c62be82010-05-06 00:08:46 +00002255 PyStructSequence_SET_ITEM(v, ST_RDEV_IDX,
2256 PyLong_FromLong((long)st->st_rdev));
Fred Drake699f3522000-06-29 21:12:41 +00002257#endif
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002258#ifdef HAVE_STRUCT_STAT_ST_GEN
Victor Stinner8c62be82010-05-06 00:08:46 +00002259 PyStructSequence_SET_ITEM(v, ST_GEN_IDX,
2260 PyLong_FromLong((long)st->st_gen));
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002261#endif
2262#ifdef HAVE_STRUCT_STAT_ST_BIRTHTIME
Victor Stinner8c62be82010-05-06 00:08:46 +00002263 {
Victor Stinner4195b5c2012-02-08 23:03:19 +01002264 PyObject *val;
2265 unsigned long bsec,bnsec;
2266 bsec = (long)st->st_birthtime;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002267#ifdef HAVE_STAT_TV_NSEC2
Victor Stinner4195b5c2012-02-08 23:03:19 +01002268 bnsec = st->st_birthtimespec.tv_nsec;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002269#else
Victor Stinner4195b5c2012-02-08 23:03:19 +01002270 bnsec = 0;
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002271#endif
Victor Stinner01b5aab2017-10-24 02:02:00 -07002272 val = PyFloat_FromDouble(bsec + 1e-9*bnsec);
Victor Stinner4195b5c2012-02-08 23:03:19 +01002273 PyStructSequence_SET_ITEM(v, ST_BIRTHTIME_IDX,
2274 val);
Victor Stinner8c62be82010-05-06 00:08:46 +00002275 }
Martin v. Löwisebd9d5b2005-08-09 15:00:59 +00002276#endif
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002277#ifdef HAVE_STRUCT_STAT_ST_FLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +00002278 PyStructSequence_SET_ITEM(v, ST_FLAGS_IDX,
2279 PyLong_FromLong((long)st->st_flags));
Hye-Shik Chang5f937a72005-06-02 13:09:30 +00002280#endif
Zachary Ware63f277b2014-06-19 09:46:37 -05002281#ifdef HAVE_STRUCT_STAT_ST_FILE_ATTRIBUTES
2282 PyStructSequence_SET_ITEM(v, ST_FILE_ATTRIBUTES_IDX,
2283 PyLong_FromUnsignedLong(st->st_file_attributes));
2284#endif
jcea6c51d512018-01-28 14:00:08 +01002285#ifdef HAVE_STRUCT_STAT_ST_FSTYPE
2286 PyStructSequence_SET_ITEM(v, ST_FSTYPE_IDX,
2287 PyUnicode_FromString(st->st_fstype));
2288#endif
Steve Dowerdf2d4a62019-08-21 15:27:33 -07002289#ifdef HAVE_STRUCT_STAT_ST_REPARSE_TAG
2290 PyStructSequence_SET_ITEM(v, ST_REPARSE_TAG_IDX,
2291 PyLong_FromUnsignedLong(st->st_reparse_tag));
2292#endif
Fred Drake699f3522000-06-29 21:12:41 +00002293
Victor Stinner8c62be82010-05-06 00:08:46 +00002294 if (PyErr_Occurred()) {
2295 Py_DECREF(v);
2296 return NULL;
2297 }
Fred Drake699f3522000-06-29 21:12:41 +00002298
Victor Stinner8c62be82010-05-06 00:08:46 +00002299 return v;
Fred Drake699f3522000-06-29 21:12:41 +00002300}
2301
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002302/* POSIX methods */
2303
Guido van Rossum94f6f721999-01-06 18:42:14 +00002304
2305static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02002306posix_do_stat(const char *function_name, path_t *path,
Larry Hastings9cf065c2012-06-22 16:30:09 -07002307 int dir_fd, int follow_symlinks)
Guido van Rossum94f6f721999-01-06 18:42:14 +00002308{
Larry Hastings9cf065c2012-06-22 16:30:09 -07002309 STRUCT_STAT st;
2310 int result;
2311
2312#if !defined(MS_WINDOWS) && !defined(HAVE_FSTATAT) && !defined(HAVE_LSTAT)
2313 if (follow_symlinks_specified(function_name, follow_symlinks))
2314 return NULL;
2315#endif
2316
2317 if (path_and_dir_fd_invalid("stat", path, dir_fd) ||
2318 dir_fd_and_fd_invalid("stat", dir_fd, path->fd) ||
2319 fd_and_follow_symlinks_invalid("stat", path->fd, follow_symlinks))
2320 return NULL;
2321
2322 Py_BEGIN_ALLOW_THREADS
2323 if (path->fd != -1)
2324 result = FSTAT(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002325#ifdef MS_WINDOWS
Steve Dower513d7472016-09-08 10:41:50 -07002326 else if (follow_symlinks)
Steve Dowercc16be82016-09-08 10:35:16 -07002327 result = win32_stat(path->wide, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002328 else
Steve Dowercc16be82016-09-08 10:35:16 -07002329 result = win32_lstat(path->wide, &st);
2330#else
2331 else
2332#if defined(HAVE_LSTAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07002333 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
2334 result = LSTAT(path->narrow, &st);
2335 else
Steve Dowercc16be82016-09-08 10:35:16 -07002336#endif /* HAVE_LSTAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002337#ifdef HAVE_FSTATAT
2338 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks)
2339 result = fstatat(dir_fd, path->narrow, &st,
2340 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
2341 else
Steve Dowercc16be82016-09-08 10:35:16 -07002342#endif /* HAVE_FSTATAT */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002343 result = STAT(path->narrow, &st);
Steve Dowercc16be82016-09-08 10:35:16 -07002344#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07002345 Py_END_ALLOW_THREADS
2346
Victor Stinner292c8352012-10-30 02:17:38 +01002347 if (result != 0) {
2348 return path_error(path);
2349 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07002350
2351 return _pystat_fromstructstat(&st);
2352}
2353
Larry Hastings2f936352014-08-05 14:04:04 +10002354/*[python input]
2355
2356for s in """
2357
2358FACCESSAT
2359FCHMODAT
2360FCHOWNAT
2361FSTATAT
2362LINKAT
2363MKDIRAT
2364MKFIFOAT
2365MKNODAT
2366OPENAT
2367READLINKAT
2368SYMLINKAT
2369UNLINKAT
2370
2371""".strip().split():
2372 s = s.strip()
2373 print("""
2374#ifdef HAVE_{s}
2375 #define {s}_DIR_FD_CONVERTER dir_fd_converter
Larry Hastings31826802013-10-19 00:09:25 -07002376#else
Larry Hastings2f936352014-08-05 14:04:04 +10002377 #define {s}_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings31826802013-10-19 00:09:25 -07002378#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002379""".rstrip().format(s=s))
2380
2381for s in """
2382
2383FCHDIR
2384FCHMOD
2385FCHOWN
2386FDOPENDIR
2387FEXECVE
2388FPATHCONF
2389FSTATVFS
2390FTRUNCATE
2391
2392""".strip().split():
2393 s = s.strip()
2394 print("""
2395#ifdef HAVE_{s}
2396 #define PATH_HAVE_{s} 1
2397#else
2398 #define PATH_HAVE_{s} 0
2399#endif
2400
2401""".rstrip().format(s=s))
2402[python start generated code]*/
2403
2404#ifdef HAVE_FACCESSAT
2405 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_converter
2406#else
2407 #define FACCESSAT_DIR_FD_CONVERTER dir_fd_unavailable
2408#endif
2409
2410#ifdef HAVE_FCHMODAT
2411 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_converter
2412#else
2413 #define FCHMODAT_DIR_FD_CONVERTER dir_fd_unavailable
2414#endif
2415
2416#ifdef HAVE_FCHOWNAT
2417 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_converter
2418#else
2419 #define FCHOWNAT_DIR_FD_CONVERTER dir_fd_unavailable
2420#endif
2421
2422#ifdef HAVE_FSTATAT
2423 #define FSTATAT_DIR_FD_CONVERTER dir_fd_converter
2424#else
2425 #define FSTATAT_DIR_FD_CONVERTER dir_fd_unavailable
2426#endif
2427
2428#ifdef HAVE_LINKAT
2429 #define LINKAT_DIR_FD_CONVERTER dir_fd_converter
2430#else
2431 #define LINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2432#endif
2433
2434#ifdef HAVE_MKDIRAT
2435 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_converter
2436#else
2437 #define MKDIRAT_DIR_FD_CONVERTER dir_fd_unavailable
2438#endif
2439
2440#ifdef HAVE_MKFIFOAT
2441 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_converter
2442#else
2443 #define MKFIFOAT_DIR_FD_CONVERTER dir_fd_unavailable
2444#endif
2445
2446#ifdef HAVE_MKNODAT
2447 #define MKNODAT_DIR_FD_CONVERTER dir_fd_converter
2448#else
2449 #define MKNODAT_DIR_FD_CONVERTER dir_fd_unavailable
2450#endif
2451
2452#ifdef HAVE_OPENAT
2453 #define OPENAT_DIR_FD_CONVERTER dir_fd_converter
2454#else
2455 #define OPENAT_DIR_FD_CONVERTER dir_fd_unavailable
2456#endif
2457
2458#ifdef HAVE_READLINKAT
2459 #define READLINKAT_DIR_FD_CONVERTER dir_fd_converter
2460#else
2461 #define READLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2462#endif
2463
2464#ifdef HAVE_SYMLINKAT
2465 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_converter
2466#else
2467 #define SYMLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2468#endif
2469
2470#ifdef HAVE_UNLINKAT
2471 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_converter
2472#else
2473 #define UNLINKAT_DIR_FD_CONVERTER dir_fd_unavailable
2474#endif
2475
2476#ifdef HAVE_FCHDIR
2477 #define PATH_HAVE_FCHDIR 1
2478#else
2479 #define PATH_HAVE_FCHDIR 0
2480#endif
2481
2482#ifdef HAVE_FCHMOD
2483 #define PATH_HAVE_FCHMOD 1
2484#else
2485 #define PATH_HAVE_FCHMOD 0
2486#endif
2487
2488#ifdef HAVE_FCHOWN
2489 #define PATH_HAVE_FCHOWN 1
2490#else
2491 #define PATH_HAVE_FCHOWN 0
2492#endif
2493
2494#ifdef HAVE_FDOPENDIR
2495 #define PATH_HAVE_FDOPENDIR 1
2496#else
2497 #define PATH_HAVE_FDOPENDIR 0
2498#endif
2499
2500#ifdef HAVE_FEXECVE
2501 #define PATH_HAVE_FEXECVE 1
2502#else
2503 #define PATH_HAVE_FEXECVE 0
2504#endif
2505
2506#ifdef HAVE_FPATHCONF
2507 #define PATH_HAVE_FPATHCONF 1
2508#else
2509 #define PATH_HAVE_FPATHCONF 0
2510#endif
2511
2512#ifdef HAVE_FSTATVFS
2513 #define PATH_HAVE_FSTATVFS 1
2514#else
2515 #define PATH_HAVE_FSTATVFS 0
2516#endif
2517
2518#ifdef HAVE_FTRUNCATE
2519 #define PATH_HAVE_FTRUNCATE 1
2520#else
2521 #define PATH_HAVE_FTRUNCATE 0
2522#endif
2523/*[python end generated code: output=4bd4f6f7d41267f1 input=80b4c890b6774ea5]*/
Larry Hastings31826802013-10-19 00:09:25 -07002524
Steve Dowerfe0a41a2015-03-20 19:50:46 -07002525#ifdef MS_WINDOWS
2526 #undef PATH_HAVE_FTRUNCATE
2527 #define PATH_HAVE_FTRUNCATE 1
2528#endif
Larry Hastings31826802013-10-19 00:09:25 -07002529
Larry Hastings61272b72014-01-07 12:41:53 -08002530/*[python input]
Larry Hastings31826802013-10-19 00:09:25 -07002531
2532class path_t_converter(CConverter):
2533
2534 type = "path_t"
2535 impl_by_reference = True
2536 parse_by_reference = True
2537
2538 converter = 'path_converter'
2539
2540 def converter_init(self, *, allow_fd=False, nullable=False):
Larry Hastings31826802013-10-19 00:09:25 -07002541 # right now path_t doesn't support default values.
2542 # to support a default value, you'll need to override initialize().
Larry Hastings2f936352014-08-05 14:04:04 +10002543 if self.default not in (unspecified, None):
Larry Hastings7726ac92014-01-31 22:03:12 -08002544 fail("Can't specify a default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002545
Larry Hastings2f936352014-08-05 14:04:04 +10002546 if self.c_default not in (None, 'Py_None'):
2547 raise RuntimeError("Can't specify a c_default to the path_t converter!")
Larry Hastings31826802013-10-19 00:09:25 -07002548
2549 self.nullable = nullable
2550 self.allow_fd = allow_fd
2551
Larry Hastings7726ac92014-01-31 22:03:12 -08002552 def pre_render(self):
2553 def strify(value):
Larry Hastings2f936352014-08-05 14:04:04 +10002554 if isinstance(value, str):
2555 return value
Larry Hastings7726ac92014-01-31 22:03:12 -08002556 return str(int(bool(value)))
2557
2558 # add self.py_name here when merging with posixmodule conversion
Larry Hastings2f936352014-08-05 14:04:04 +10002559 self.c_default = 'PATH_T_INITIALIZE("{}", "{}", {}, {})'.format(
Larry Hastings31826802013-10-19 00:09:25 -07002560 self.function.name,
Larry Hastings2f936352014-08-05 14:04:04 +10002561 self.name,
Larry Hastings7726ac92014-01-31 22:03:12 -08002562 strify(self.nullable),
2563 strify(self.allow_fd),
Larry Hastings31826802013-10-19 00:09:25 -07002564 )
2565
2566 def cleanup(self):
2567 return "path_cleanup(&" + self.name + ");\n"
2568
2569
2570class dir_fd_converter(CConverter):
2571 type = 'int'
Larry Hastings31826802013-10-19 00:09:25 -07002572
Larry Hastings2f936352014-08-05 14:04:04 +10002573 def converter_init(self, requires=None):
Larry Hastings31826802013-10-19 00:09:25 -07002574 if self.default in (unspecified, None):
2575 self.c_default = 'DEFAULT_DIR_FD'
Larry Hastings2f936352014-08-05 14:04:04 +10002576 if isinstance(requires, str):
2577 self.converter = requires.upper() + '_DIR_FD_CONVERTER'
2578 else:
2579 self.converter = 'dir_fd_converter'
Larry Hastings31826802013-10-19 00:09:25 -07002580
Larry Hastings2f936352014-08-05 14:04:04 +10002581class fildes_converter(CConverter):
2582 type = 'int'
2583 converter = 'fildes_converter'
2584
2585class uid_t_converter(CConverter):
2586 type = "uid_t"
2587 converter = '_Py_Uid_Converter'
2588
2589class gid_t_converter(CConverter):
2590 type = "gid_t"
2591 converter = '_Py_Gid_Converter'
2592
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02002593class dev_t_converter(CConverter):
2594 type = 'dev_t'
2595 converter = '_Py_Dev_Converter'
2596
2597class dev_t_return_converter(unsigned_long_return_converter):
2598 type = 'dev_t'
2599 conversion_fn = '_PyLong_FromDev'
2600 unsigned_cast = '(dev_t)'
2601
Larry Hastings2f936352014-08-05 14:04:04 +10002602class FSConverter_converter(CConverter):
2603 type = 'PyObject *'
2604 converter = 'PyUnicode_FSConverter'
2605 def converter_init(self):
2606 if self.default is not unspecified:
2607 fail("FSConverter_converter does not support default values")
2608 self.c_default = 'NULL'
2609
2610 def cleanup(self):
2611 return "Py_XDECREF(" + self.name + ");\n"
2612
2613class pid_t_converter(CConverter):
2614 type = 'pid_t'
2615 format_unit = '" _Py_PARSE_PID "'
2616
2617class idtype_t_converter(int_converter):
2618 type = 'idtype_t'
2619
2620class id_t_converter(CConverter):
2621 type = 'id_t'
2622 format_unit = '" _Py_PARSE_PID "'
2623
Benjamin Petersonca470632016-09-06 13:47:26 -07002624class intptr_t_converter(CConverter):
2625 type = 'intptr_t'
Larry Hastings2f936352014-08-05 14:04:04 +10002626 format_unit = '" _Py_PARSE_INTPTR "'
2627
2628class Py_off_t_converter(CConverter):
2629 type = 'Py_off_t'
2630 converter = 'Py_off_t_converter'
2631
2632class Py_off_t_return_converter(long_return_converter):
2633 type = 'Py_off_t'
2634 conversion_fn = 'PyLong_FromPy_off_t'
2635
2636class path_confname_converter(CConverter):
2637 type="int"
2638 converter="conv_path_confname"
2639
2640class confstr_confname_converter(path_confname_converter):
2641 converter='conv_confstr_confname'
2642
2643class sysconf_confname_converter(path_confname_converter):
2644 converter="conv_sysconf_confname"
2645
2646class sched_param_converter(CConverter):
2647 type = 'struct sched_param'
2648 converter = 'convert_sched_param'
2649 impl_by_reference = True;
Larry Hastings31826802013-10-19 00:09:25 -07002650
Larry Hastings61272b72014-01-07 12:41:53 -08002651[python start generated code]*/
Victor Stinner581139c2016-09-06 15:54:20 -07002652/*[python end generated code: output=da39a3ee5e6b4b0d input=418fce0e01144461]*/
Larry Hastings31826802013-10-19 00:09:25 -07002653
Larry Hastings61272b72014-01-07 12:41:53 -08002654/*[clinic input]
Larry Hastings31826802013-10-19 00:09:25 -07002655
Larry Hastings2a727912014-01-16 11:32:01 -08002656os.stat
Larry Hastings31826802013-10-19 00:09:25 -07002657
2658 path : path_t(allow_fd=True)
BNMetricsb9427072018-11-02 15:20:19 +00002659 Path to be examined; can be string, bytes, a path-like object or
Xiang Zhang4459e002017-01-22 13:04:17 +08002660 open-file-descriptor int.
Larry Hastings31826802013-10-19 00:09:25 -07002661
2662 *
2663
Larry Hastings2f936352014-08-05 14:04:04 +10002664 dir_fd : dir_fd(requires='fstatat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002665 If not None, it should be a file descriptor open to a directory,
2666 and path should be a relative string; path will then be relative to
2667 that directory.
2668
2669 follow_symlinks: bool = True
2670 If False, and the last element of the path is a symbolic link,
2671 stat will examine the symbolic link itself instead of the file
2672 the link points to.
2673
2674Perform a stat system call on the given path.
2675
2676dir_fd and follow_symlinks may not be implemented
2677 on your platform. If they are unavailable, using them will raise a
2678 NotImplementedError.
2679
2680It's an error to use dir_fd or follow_symlinks when specifying path as
2681 an open file descriptor.
2682
Larry Hastings61272b72014-01-07 12:41:53 -08002683[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002684
Larry Hastings31826802013-10-19 00:09:25 -07002685static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002686os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002687/*[clinic end generated code: output=7d4976e6f18a59c5 input=01d362ebcc06996b]*/
Larry Hastings31826802013-10-19 00:09:25 -07002688{
2689 return posix_do_stat("stat", path, dir_fd, follow_symlinks);
2690}
2691
Larry Hastings2f936352014-08-05 14:04:04 +10002692
2693/*[clinic input]
2694os.lstat
2695
2696 path : path_t
2697
2698 *
2699
2700 dir_fd : dir_fd(requires='fstatat') = None
2701
2702Perform a stat system call on the given path, without following symbolic links.
2703
2704Like stat(), but do not follow symbolic links.
2705Equivalent to stat(path, follow_symlinks=False).
2706[clinic start generated code]*/
2707
Larry Hastings2f936352014-08-05 14:04:04 +10002708static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002709os_lstat_impl(PyObject *module, path_t *path, int dir_fd)
2710/*[clinic end generated code: output=ef82a5d35ce8ab37 input=0b7474765927b925]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002711{
2712 int follow_symlinks = 0;
2713 return posix_do_stat("lstat", path, dir_fd, follow_symlinks);
2714}
Larry Hastings31826802013-10-19 00:09:25 -07002715
Larry Hastings2f936352014-08-05 14:04:04 +10002716
Larry Hastings61272b72014-01-07 12:41:53 -08002717/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10002718os.access -> bool
Larry Hastings31826802013-10-19 00:09:25 -07002719
Benjamin Peterson768f3b42016-09-05 15:29:33 -07002720 path: path_t
BNMetricsb9427072018-11-02 15:20:19 +00002721 Path to be tested; can be string, bytes, or a path-like object.
Larry Hastings31826802013-10-19 00:09:25 -07002722
2723 mode: int
2724 Operating-system mode bitfield. Can be F_OK to test existence,
2725 or the inclusive-OR of R_OK, W_OK, and X_OK.
2726
2727 *
2728
Larry Hastings2f936352014-08-05 14:04:04 +10002729 dir_fd : dir_fd(requires='faccessat') = None
Larry Hastings31826802013-10-19 00:09:25 -07002730 If not None, it should be a file descriptor open to a directory,
2731 and path should be relative; path will then be relative to that
2732 directory.
2733
2734 effective_ids: bool = False
2735 If True, access will use the effective uid/gid instead of
2736 the real uid/gid.
2737
2738 follow_symlinks: bool = True
2739 If False, and the last element of the path is a symbolic link,
2740 access will examine the symbolic link itself instead of the file
2741 the link points to.
2742
2743Use the real uid/gid to test for access to a path.
2744
2745{parameters}
2746dir_fd, effective_ids, and follow_symlinks may not be implemented
2747 on your platform. If they are unavailable, using them will raise a
2748 NotImplementedError.
2749
2750Note that most operations will use the effective uid/gid, therefore this
2751 routine can be used in a suid/sgid environment to test if the invoking user
2752 has the specified access to the path.
2753
Larry Hastings61272b72014-01-07 12:41:53 -08002754[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002755
Larry Hastings2f936352014-08-05 14:04:04 +10002756static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002757os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04002758 int effective_ids, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00002759/*[clinic end generated code: output=cf84158bc90b1a77 input=3ffe4e650ee3bf20]*/
Larry Hastings31826802013-10-19 00:09:25 -07002760{
Larry Hastings2f936352014-08-05 14:04:04 +10002761 int return_value;
Victor Stinner8c62be82010-05-06 00:08:46 +00002762
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00002763#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00002764 DWORD attr;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002765#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07002766 int result;
Thomas Wouters477c8d52006-05-27 19:21:47 +00002767#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07002768
Larry Hastings9cf065c2012-06-22 16:30:09 -07002769#ifndef HAVE_FACCESSAT
2770 if (follow_symlinks_specified("access", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10002771 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002772
2773 if (effective_ids) {
2774 argument_unavailable_error("access", "effective_ids");
Larry Hastings2f936352014-08-05 14:04:04 +10002775 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002776 }
2777#endif
2778
2779#ifdef MS_WINDOWS
2780 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07002781 attr = GetFileAttributesW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002782 Py_END_ALLOW_THREADS
2783
2784 /*
Georg Brandlf7875592012-06-24 13:58:31 +02002785 * Access is possible if
Larry Hastings9cf065c2012-06-22 16:30:09 -07002786 * * we didn't get a -1, and
2787 * * write access wasn't requested,
2788 * * or the file isn't read-only,
2789 * * or it's a directory.
2790 * (Directories cannot be read-only on Windows.)
2791 */
Larry Hastings2f936352014-08-05 14:04:04 +10002792 return_value = (attr != INVALID_FILE_ATTRIBUTES) &&
Georg Brandl5bb7aa92012-06-23 12:48:40 +02002793 (!(mode & 2) ||
Larry Hastings9cf065c2012-06-22 16:30:09 -07002794 !(attr & FILE_ATTRIBUTE_READONLY) ||
Larry Hastings2f936352014-08-05 14:04:04 +10002795 (attr & FILE_ATTRIBUTE_DIRECTORY));
Larry Hastings9cf065c2012-06-22 16:30:09 -07002796#else
2797
2798 Py_BEGIN_ALLOW_THREADS
2799#ifdef HAVE_FACCESSAT
2800 if ((dir_fd != DEFAULT_DIR_FD) ||
2801 effective_ids ||
2802 !follow_symlinks) {
2803 int flags = 0;
2804 if (!follow_symlinks)
2805 flags |= AT_SYMLINK_NOFOLLOW;
2806 if (effective_ids)
2807 flags |= AT_EACCESS;
Larry Hastings31826802013-10-19 00:09:25 -07002808 result = faccessat(dir_fd, path->narrow, mode, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002809 }
2810 else
2811#endif
Larry Hastings31826802013-10-19 00:09:25 -07002812 result = access(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002813 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10002814 return_value = !result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002815#endif
2816
Larry Hastings9cf065c2012-06-22 16:30:09 -07002817 return return_value;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002818}
2819
Guido van Rossumd371ff11999-01-25 16:12:23 +00002820#ifndef F_OK
2821#define F_OK 0
2822#endif
2823#ifndef R_OK
2824#define R_OK 4
2825#endif
2826#ifndef W_OK
2827#define W_OK 2
2828#endif
2829#ifndef X_OK
2830#define X_OK 1
2831#endif
2832
Larry Hastings31826802013-10-19 00:09:25 -07002833
Guido van Rossumd371ff11999-01-25 16:12:23 +00002834#ifdef HAVE_TTYNAME
Larry Hastings61272b72014-01-07 12:41:53 -08002835/*[clinic input]
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002836os.ttyname
Larry Hastings31826802013-10-19 00:09:25 -07002837
2838 fd: int
2839 Integer file descriptor handle.
2840
2841 /
2842
2843Return the name of the terminal device connected to 'fd'.
Larry Hastings61272b72014-01-07 12:41:53 -08002844[clinic start generated code]*/
Larry Hastings31826802013-10-19 00:09:25 -07002845
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002846static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002847os_ttyname_impl(PyObject *module, int fd)
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002848/*[clinic end generated code: output=c424d2e9d1cd636a input=9ff5a58b08115c55]*/
Larry Hastings31826802013-10-19 00:09:25 -07002849{
Guido van Rossum94f6f721999-01-06 18:42:14 +00002850
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002851 long size = sysconf(_SC_TTY_NAME_MAX);
2852 if (size == -1) {
Serhiy Storchaka4db62e12018-12-17 16:47:45 +02002853 return posix_error();
2854 }
Antonio Gutierrez594e2ed2019-10-09 04:19:48 +02002855 char *buffer = (char *)PyMem_RawMalloc(size);
2856 if (buffer == NULL) {
2857 return PyErr_NoMemory();
2858 }
2859 int ret = ttyname_r(fd, buffer, size);
2860 if (ret != 0) {
2861 PyMem_RawFree(buffer);
2862 errno = ret;
2863 return posix_error();
2864 }
2865 PyObject *res = PyUnicode_DecodeFSDefault(buffer);
2866 PyMem_RawFree(buffer);
2867 return res;
Guido van Rossum94f6f721999-01-06 18:42:14 +00002868}
Guido van Rossumd371ff11999-01-25 16:12:23 +00002869#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +00002870
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002871#ifdef HAVE_CTERMID
Larry Hastings2f936352014-08-05 14:04:04 +10002872/*[clinic input]
2873os.ctermid
2874
2875Return the name of the controlling terminal for this process.
2876[clinic start generated code]*/
2877
Larry Hastings2f936352014-08-05 14:04:04 +10002878static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002879os_ctermid_impl(PyObject *module)
2880/*[clinic end generated code: output=02f017e6c9e620db input=3b87fdd52556382d]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002881{
Victor Stinner8c62be82010-05-06 00:08:46 +00002882 char *ret;
2883 char buffer[L_ctermid];
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002884
Greg Wardb48bc172000-03-01 21:51:56 +00002885#ifdef USE_CTERMID_R
Victor Stinner8c62be82010-05-06 00:08:46 +00002886 ret = ctermid_r(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002887#else
Victor Stinner8c62be82010-05-06 00:08:46 +00002888 ret = ctermid(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002889#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00002890 if (ret == NULL)
2891 return posix_error();
Victor Stinner5fe6de82010-08-15 09:12:51 +00002892 return PyUnicode_DecodeFSDefault(buffer);
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002893}
Larry Hastings2f936352014-08-05 14:04:04 +10002894#endif /* HAVE_CTERMID */
Fred Drake5ab8eaf1999-12-09 21:13:07 +00002895
Larry Hastings2f936352014-08-05 14:04:04 +10002896
2897/*[clinic input]
2898os.chdir
2899
2900 path: path_t(allow_fd='PATH_HAVE_FCHDIR')
2901
2902Change the current working directory to the specified path.
2903
2904path may always be specified as a string.
2905On some platforms, path may also be specified as an open file descriptor.
2906 If this functionality is unavailable, using it raises an exception.
2907[clinic start generated code]*/
2908
Larry Hastings2f936352014-08-05 14:04:04 +10002909static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002910os_chdir_impl(PyObject *module, path_t *path)
2911/*[clinic end generated code: output=3be6400eee26eaae input=1a4a15b4d12cb15d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10002912{
2913 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07002914
Saiyang Gou7514f4f2020-02-12 23:47:42 -08002915 if (PySys_Audit("os.chdir", "(O)", path->object) < 0) {
2916 return NULL;
2917 }
2918
Larry Hastings9cf065c2012-06-22 16:30:09 -07002919 Py_BEGIN_ALLOW_THREADS
2920#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07002921 /* on unix, success = 0, on windows, success = !0 */
2922 result = !win32_wchdir(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002923#else
2924#ifdef HAVE_FCHDIR
Larry Hastings2f936352014-08-05 14:04:04 +10002925 if (path->fd != -1)
2926 result = fchdir(path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002927 else
2928#endif
Larry Hastings2f936352014-08-05 14:04:04 +10002929 result = chdir(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002930#endif
2931 Py_END_ALLOW_THREADS
2932
2933 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +10002934 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07002935 }
2936
Larry Hastings2f936352014-08-05 14:04:04 +10002937 Py_RETURN_NONE;
2938}
2939
2940
2941#ifdef HAVE_FCHDIR
2942/*[clinic input]
2943os.fchdir
2944
2945 fd: fildes
2946
2947Change to the directory of the given file descriptor.
2948
2949fd must be opened on a directory, not a file.
2950Equivalent to os.chdir(fd).
2951
2952[clinic start generated code]*/
2953
Fred Drake4d1e64b2002-04-15 19:40:07 +00002954static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002955os_fchdir_impl(PyObject *module, int fd)
2956/*[clinic end generated code: output=42e064ec4dc00ab0 input=18e816479a2fa985]*/
Fred Drake4d1e64b2002-04-15 19:40:07 +00002957{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08002958 if (PySys_Audit("os.chdir", "(i)", fd) < 0) {
2959 return NULL;
2960 }
Larry Hastings2f936352014-08-05 14:04:04 +10002961 return posix_fildes_fd(fd, fchdir);
Fred Drake4d1e64b2002-04-15 19:40:07 +00002962}
2963#endif /* HAVE_FCHDIR */
2964
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00002965
Larry Hastings2f936352014-08-05 14:04:04 +10002966/*[clinic input]
2967os.chmod
2968
2969 path: path_t(allow_fd='PATH_HAVE_FCHMOD')
BNMetricsb9427072018-11-02 15:20:19 +00002970 Path to be modified. May always be specified as a str, bytes, or a path-like object.
Larry Hastings2f936352014-08-05 14:04:04 +10002971 On some platforms, path may also be specified as an open file descriptor.
2972 If this functionality is unavailable, using it raises an exception.
2973
2974 mode: int
2975 Operating-system mode bitfield.
2976
2977 *
2978
2979 dir_fd : dir_fd(requires='fchmodat') = None
2980 If not None, it should be a file descriptor open to a directory,
2981 and path should be relative; path will then be relative to that
2982 directory.
2983
2984 follow_symlinks: bool = True
2985 If False, and the last element of the path is a symbolic link,
2986 chmod will modify the symbolic link itself instead of the file
2987 the link points to.
2988
2989Change the access permissions of a file.
2990
2991It is an error to use dir_fd or follow_symlinks when specifying path as
2992 an open file descriptor.
2993dir_fd and follow_symlinks may not be implemented on your platform.
2994 If they are unavailable, using them will raise a NotImplementedError.
2995
2996[clinic start generated code]*/
2997
Larry Hastings2f936352014-08-05 14:04:04 +10002998static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002999os_chmod_impl(PyObject *module, path_t *path, int mode, int dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003000 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003001/*[clinic end generated code: output=5cf6a94915cc7bff input=989081551c00293b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003002{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003003 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003004
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003005#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003006 DWORD attr;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003007#endif
Hirokazu Yamamoto892a37a2009-06-28 11:07:03 +00003008
Larry Hastings9cf065c2012-06-22 16:30:09 -07003009#ifdef HAVE_FCHMODAT
3010 int fchmodat_nofollow_unsupported = 0;
3011#endif
3012
Larry Hastings9cf065c2012-06-22 16:30:09 -07003013#if !(defined(HAVE_FCHMODAT) || defined(HAVE_LCHMOD))
3014 if (follow_symlinks_specified("chmod", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003015 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003016#endif
3017
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003018 if (PySys_Audit("os.chmod", "Oii", path->object, mode,
3019 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3020 return NULL;
3021 }
3022
Larry Hastings9cf065c2012-06-22 16:30:09 -07003023#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00003024 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003025 attr = GetFileAttributesW(path->wide);
Tim Golden23005082013-10-25 11:22:37 +01003026 if (attr == INVALID_FILE_ATTRIBUTES)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003027 result = 0;
3028 else {
3029 if (mode & _S_IWRITE)
Victor Stinner8c62be82010-05-06 00:08:46 +00003030 attr &= ~FILE_ATTRIBUTE_READONLY;
3031 else
3032 attr |= FILE_ATTRIBUTE_READONLY;
Steve Dowercc16be82016-09-08 10:35:16 -07003033 result = SetFileAttributesW(path->wide, attr);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003034 }
3035 Py_END_ALLOW_THREADS
3036
3037 if (!result) {
Larry Hastings2f936352014-08-05 14:04:04 +10003038 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003039 }
3040#else /* MS_WINDOWS */
3041 Py_BEGIN_ALLOW_THREADS
3042#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003043 if (path->fd != -1)
3044 result = fchmod(path->fd, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003045 else
3046#endif
3047#ifdef HAVE_LCHMOD
3048 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003049 result = lchmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003050 else
3051#endif
3052#ifdef HAVE_FCHMODAT
3053 if ((dir_fd != DEFAULT_DIR_FD) || !follow_symlinks) {
3054 /*
3055 * fchmodat() doesn't currently support AT_SYMLINK_NOFOLLOW!
3056 * The documentation specifically shows how to use it,
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003057 * and then says it isn't implemented yet.
3058 * (true on linux with glibc 2.15, and openindiana 3.x)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003059 *
3060 * Once it is supported, os.chmod will automatically
3061 * support dir_fd and follow_symlinks=False. (Hopefully.)
3062 * Until then, we need to be careful what exception we raise.
3063 */
Larry Hastings2f936352014-08-05 14:04:04 +10003064 result = fchmodat(dir_fd, path->narrow, mode,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003065 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3066 /*
3067 * But wait! We can't throw the exception without allowing threads,
3068 * and we can't do that in this nested scope. (Macro trickery, sigh.)
3069 */
3070 fchmodat_nofollow_unsupported =
Larry Hastingsdbbc0c82012-06-22 19:50:21 -07003071 result &&
3072 ((errno == ENOTSUP) || (errno == EOPNOTSUPP)) &&
3073 !follow_symlinks;
Victor Stinner8c62be82010-05-06 00:08:46 +00003074 }
3075 else
Thomas Wouters477c8d52006-05-27 19:21:47 +00003076#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003077 result = chmod(path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003078 Py_END_ALLOW_THREADS
3079
3080 if (result) {
3081#ifdef HAVE_FCHMODAT
3082 if (fchmodat_nofollow_unsupported) {
3083 if (dir_fd != DEFAULT_DIR_FD)
3084 dir_fd_and_follow_symlinks_invalid("chmod",
3085 dir_fd, follow_symlinks);
3086 else
3087 follow_symlinks_specified("chmod", follow_symlinks);
Anthony Sottile233ef242017-12-14 08:57:55 -08003088 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003089 }
3090 else
3091#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003092 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003093 }
3094#endif
3095
Larry Hastings2f936352014-08-05 14:04:04 +10003096 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003097}
3098
Larry Hastings9cf065c2012-06-22 16:30:09 -07003099
Christian Heimes4e30a842007-11-30 22:12:06 +00003100#ifdef HAVE_FCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003101/*[clinic input]
3102os.fchmod
3103
3104 fd: int
3105 mode: int
3106
3107Change the access permissions of the file given by file descriptor fd.
3108
3109Equivalent to os.chmod(fd, mode).
3110[clinic start generated code]*/
3111
Larry Hastings2f936352014-08-05 14:04:04 +10003112static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003113os_fchmod_impl(PyObject *module, int fd, int mode)
3114/*[clinic end generated code: output=afd9bc05b4e426b3 input=8ab11975ca01ee5b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003115{
3116 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003117 int async_err = 0;
3118
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003119 if (PySys_Audit("os.chmod", "iii", fd, mode, -1) < 0) {
3120 return NULL;
3121 }
3122
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003123 do {
3124 Py_BEGIN_ALLOW_THREADS
3125 res = fchmod(fd, mode);
3126 Py_END_ALLOW_THREADS
3127 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3128 if (res != 0)
3129 return (!async_err) ? posix_error() : NULL;
3130
Victor Stinner8c62be82010-05-06 00:08:46 +00003131 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003132}
3133#endif /* HAVE_FCHMOD */
3134
Larry Hastings2f936352014-08-05 14:04:04 +10003135
Christian Heimes4e30a842007-11-30 22:12:06 +00003136#ifdef HAVE_LCHMOD
Larry Hastings2f936352014-08-05 14:04:04 +10003137/*[clinic input]
3138os.lchmod
3139
3140 path: path_t
3141 mode: int
3142
3143Change the access permissions of a file, without following symbolic links.
3144
3145If path is a symlink, this affects the link itself rather than the target.
3146Equivalent to chmod(path, mode, follow_symlinks=False)."
3147[clinic start generated code]*/
3148
Larry Hastings2f936352014-08-05 14:04:04 +10003149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003150os_lchmod_impl(PyObject *module, path_t *path, int mode)
3151/*[clinic end generated code: output=082344022b51a1d5 input=90c5663c7465d24f]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003152{
Victor Stinner8c62be82010-05-06 00:08:46 +00003153 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003154 if (PySys_Audit("os.chmod", "Oii", path->object, mode, -1) < 0) {
3155 return NULL;
3156 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003157 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003158 res = lchmod(path->narrow, mode);
Victor Stinner8c62be82010-05-06 00:08:46 +00003159 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003160 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003161 path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003162 return NULL;
3163 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003164 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003165}
3166#endif /* HAVE_LCHMOD */
3167
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003168
Thomas Wouterscf297e42007-02-23 15:07:44 +00003169#ifdef HAVE_CHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003170/*[clinic input]
3171os.chflags
3172
3173 path: path_t
3174 flags: unsigned_long(bitwise=True)
3175 follow_symlinks: bool=True
3176
3177Set file flags.
3178
3179If follow_symlinks is False, and the last element of the path is a symbolic
3180 link, chflags will change flags on the symbolic link itself instead of the
3181 file the link points to.
3182follow_symlinks may not be implemented on your platform. If it is
3183unavailable, using it will raise a NotImplementedError.
3184
3185[clinic start generated code]*/
3186
Larry Hastings2f936352014-08-05 14:04:04 +10003187static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003188os_chflags_impl(PyObject *module, path_t *path, unsigned long flags,
Larry Hastings89964c42015-04-14 18:07:59 -04003189 int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003190/*[clinic end generated code: output=85571c6737661ce9 input=0327e29feb876236]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003191{
3192 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003193
3194#ifndef HAVE_LCHFLAGS
3195 if (follow_symlinks_specified("chflags", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003196 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003197#endif
3198
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003199 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3200 return NULL;
3201 }
3202
Victor Stinner8c62be82010-05-06 00:08:46 +00003203 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003204#ifdef HAVE_LCHFLAGS
3205 if (!follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +10003206 result = lchflags(path->narrow, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003207 else
3208#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003209 result = chflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003210 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003211
Larry Hastings2f936352014-08-05 14:04:04 +10003212 if (result)
3213 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003214
Larry Hastings2f936352014-08-05 14:04:04 +10003215 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003216}
3217#endif /* HAVE_CHFLAGS */
3218
Larry Hastings2f936352014-08-05 14:04:04 +10003219
Thomas Wouterscf297e42007-02-23 15:07:44 +00003220#ifdef HAVE_LCHFLAGS
Larry Hastings2f936352014-08-05 14:04:04 +10003221/*[clinic input]
3222os.lchflags
3223
3224 path: path_t
3225 flags: unsigned_long(bitwise=True)
3226
3227Set file flags.
3228
3229This function will not follow symbolic links.
3230Equivalent to chflags(path, flags, follow_symlinks=False).
3231[clinic start generated code]*/
3232
Larry Hastings2f936352014-08-05 14:04:04 +10003233static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003234os_lchflags_impl(PyObject *module, path_t *path, unsigned long flags)
3235/*[clinic end generated code: output=30ae958695c07316 input=f9f82ea8b585ca9d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003236{
Victor Stinner8c62be82010-05-06 00:08:46 +00003237 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003238 if (PySys_Audit("os.chflags", "Ok", path->object, flags) < 0) {
3239 return NULL;
3240 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003241 Py_BEGIN_ALLOW_THREADS
Larry Hastingsb1dc1122014-08-05 16:06:16 +10003242 res = lchflags(path->narrow, flags);
Victor Stinner8c62be82010-05-06 00:08:46 +00003243 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003244 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003245 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003246 }
Victor Stinner292c8352012-10-30 02:17:38 +01003247 Py_RETURN_NONE;
Thomas Wouterscf297e42007-02-23 15:07:44 +00003248}
3249#endif /* HAVE_LCHFLAGS */
3250
Larry Hastings2f936352014-08-05 14:04:04 +10003251
Martin v. Löwis244edc82001-10-04 22:44:26 +00003252#ifdef HAVE_CHROOT
Larry Hastings2f936352014-08-05 14:04:04 +10003253/*[clinic input]
3254os.chroot
3255 path: path_t
3256
3257Change root directory to path.
3258
3259[clinic start generated code]*/
3260
Larry Hastings2f936352014-08-05 14:04:04 +10003261static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003262os_chroot_impl(PyObject *module, path_t *path)
3263/*[clinic end generated code: output=de80befc763a4475 input=14822965652c3dc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003264{
3265 int res;
3266 Py_BEGIN_ALLOW_THREADS
3267 res = chroot(path->narrow);
3268 Py_END_ALLOW_THREADS
3269 if (res < 0)
3270 return path_error(path);
3271 Py_RETURN_NONE;
3272}
3273#endif /* HAVE_CHROOT */
3274
Martin v. Löwis244edc82001-10-04 22:44:26 +00003275
Guido van Rossum21142a01999-01-08 21:05:37 +00003276#ifdef HAVE_FSYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003277/*[clinic input]
3278os.fsync
3279
3280 fd: fildes
3281
3282Force write of fd to disk.
3283[clinic start generated code]*/
3284
Larry Hastings2f936352014-08-05 14:04:04 +10003285static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003286os_fsync_impl(PyObject *module, int fd)
3287/*[clinic end generated code: output=4a10d773f52b3584 input=21c3645c056967f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003288{
3289 return posix_fildes_fd(fd, fsync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003290}
3291#endif /* HAVE_FSYNC */
3292
Larry Hastings2f936352014-08-05 14:04:04 +10003293
Ross Lagerwall7807c352011-03-17 20:20:30 +02003294#ifdef HAVE_SYNC
Larry Hastings2f936352014-08-05 14:04:04 +10003295/*[clinic input]
3296os.sync
3297
3298Force write of everything to disk.
3299[clinic start generated code]*/
3300
Larry Hastings2f936352014-08-05 14:04:04 +10003301static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003302os_sync_impl(PyObject *module)
3303/*[clinic end generated code: output=2796b1f0818cd71c input=84749fe5e9b404ff]*/
Ross Lagerwall7807c352011-03-17 20:20:30 +02003304{
3305 Py_BEGIN_ALLOW_THREADS
3306 sync();
3307 Py_END_ALLOW_THREADS
3308 Py_RETURN_NONE;
3309}
Larry Hastings2f936352014-08-05 14:04:04 +10003310#endif /* HAVE_SYNC */
3311
Ross Lagerwall7807c352011-03-17 20:20:30 +02003312
Guido van Rossum21142a01999-01-08 21:05:37 +00003313#ifdef HAVE_FDATASYNC
Guido van Rossum7f58e2e2000-09-22 17:26:14 +00003314#ifdef __hpux
Guido van Rossumecc23b02000-09-22 16:01:05 +00003315extern int fdatasync(int); /* On HP-UX, in libc but not in unistd.h */
3316#endif
3317
Larry Hastings2f936352014-08-05 14:04:04 +10003318/*[clinic input]
3319os.fdatasync
3320
3321 fd: fildes
3322
3323Force write of fd to disk without forcing update of metadata.
3324[clinic start generated code]*/
3325
Larry Hastings2f936352014-08-05 14:04:04 +10003326static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003327os_fdatasync_impl(PyObject *module, int fd)
3328/*[clinic end generated code: output=b4b9698b5d7e26dd input=bc74791ee54dd291]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003329{
3330 return posix_fildes_fd(fd, fdatasync);
Guido van Rossum21142a01999-01-08 21:05:37 +00003331}
3332#endif /* HAVE_FDATASYNC */
3333
3334
Fredrik Lundh10723342000-07-10 16:38:09 +00003335#ifdef HAVE_CHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003336/*[clinic input]
3337os.chown
3338
3339 path : path_t(allow_fd='PATH_HAVE_FCHOWN')
BNMetricsb9427072018-11-02 15:20:19 +00003340 Path to be examined; can be string, bytes, a path-like object, or open-file-descriptor int.
Larry Hastings2f936352014-08-05 14:04:04 +10003341
3342 uid: uid_t
3343
3344 gid: gid_t
3345
3346 *
3347
3348 dir_fd : dir_fd(requires='fchownat') = None
3349 If not None, it should be a file descriptor open to a directory,
3350 and path should be relative; path will then be relative to that
3351 directory.
3352
3353 follow_symlinks: bool = True
3354 If False, and the last element of the path is a symbolic link,
3355 stat will examine the symbolic link itself instead of the file
3356 the link points to.
3357
3358Change the owner and group id of path to the numeric uid and gid.\
3359
3360path may always be specified as a string.
3361On some platforms, path may also be specified as an open file descriptor.
3362 If this functionality is unavailable, using it raises an exception.
3363If dir_fd is not None, it should be a file descriptor open to a directory,
3364 and path should be relative; path will then be relative to that directory.
3365If follow_symlinks is False, and the last element of the path is a symbolic
3366 link, chown will modify the symbolic link itself instead of the file the
3367 link points to.
3368It is an error to use dir_fd or follow_symlinks when specifying path as
3369 an open file descriptor.
3370dir_fd and follow_symlinks may not be implemented on your platform.
3371 If they are unavailable, using them will raise a NotImplementedError.
3372
3373[clinic start generated code]*/
3374
Larry Hastings2f936352014-08-05 14:04:04 +10003375static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003376os_chown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid,
Larry Hastings89964c42015-04-14 18:07:59 -04003377 int dir_fd, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +00003378/*[clinic end generated code: output=4beadab0db5f70cd input=b08c5ec67996a97d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003379{
3380 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003381
3382#if !(defined(HAVE_LCHOWN) || defined(HAVE_FCHOWNAT))
3383 if (follow_symlinks_specified("chown", follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003384 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003385#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003386 if (dir_fd_and_fd_invalid("chown", dir_fd, path->fd) ||
3387 fd_and_follow_symlinks_invalid("chown", path->fd, follow_symlinks))
3388 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003389
3390#ifdef __APPLE__
3391 /*
3392 * This is for Mac OS X 10.3, which doesn't have lchown.
3393 * (But we still have an lchown symbol because of weak-linking.)
3394 * It doesn't have fchownat either. So there's no possibility
3395 * of a graceful failover.
Georg Brandlf7875592012-06-24 13:58:31 +02003396 */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003397 if ((!follow_symlinks) && (lchown == NULL)) {
3398 follow_symlinks_specified("chown", follow_symlinks);
Larry Hastings2f936352014-08-05 14:04:04 +10003399 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003400 }
3401#endif
3402
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003403 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid,
3404 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
3405 return NULL;
3406 }
3407
Victor Stinner8c62be82010-05-06 00:08:46 +00003408 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003409#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003410 if (path->fd != -1)
3411 result = fchown(path->fd, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003412 else
3413#endif
3414#ifdef HAVE_LCHOWN
3415 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10003416 result = lchown(path->narrow, uid, gid);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003417 else
3418#endif
3419#ifdef HAVE_FCHOWNAT
3420 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003421 result = fchownat(dir_fd, path->narrow, uid, gid,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003422 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
3423 else
3424#endif
Larry Hastings2f936352014-08-05 14:04:04 +10003425 result = chown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003426 Py_END_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003427
Larry Hastings2f936352014-08-05 14:04:04 +10003428 if (result)
3429 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003430
Larry Hastings2f936352014-08-05 14:04:04 +10003431 Py_RETURN_NONE;
Guido van Rossumb6775db1994-08-01 11:34:53 +00003432}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003433#endif /* HAVE_CHOWN */
Guido van Rossumb6775db1994-08-01 11:34:53 +00003434
Larry Hastings2f936352014-08-05 14:04:04 +10003435
Christian Heimes4e30a842007-11-30 22:12:06 +00003436#ifdef HAVE_FCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003437/*[clinic input]
3438os.fchown
3439
3440 fd: int
3441 uid: uid_t
3442 gid: gid_t
3443
3444Change the owner and group id of the file specified by file descriptor.
3445
3446Equivalent to os.chown(fd, uid, gid).
3447
3448[clinic start generated code]*/
3449
Larry Hastings2f936352014-08-05 14:04:04 +10003450static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003451os_fchown_impl(PyObject *module, int fd, uid_t uid, gid_t gid)
3452/*[clinic end generated code: output=97d21cbd5a4350a6 input=3af544ba1b13a0d7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003453{
Victor Stinner8c62be82010-05-06 00:08:46 +00003454 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003455 int async_err = 0;
3456
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003457 if (PySys_Audit("os.chown", "iIIi", fd, uid, gid, -1) < 0) {
3458 return NULL;
3459 }
3460
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00003461 do {
3462 Py_BEGIN_ALLOW_THREADS
3463 res = fchown(fd, uid, gid);
3464 Py_END_ALLOW_THREADS
3465 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
3466 if (res != 0)
3467 return (!async_err) ? posix_error() : NULL;
3468
Victor Stinner8c62be82010-05-06 00:08:46 +00003469 Py_RETURN_NONE;
Christian Heimes4e30a842007-11-30 22:12:06 +00003470}
3471#endif /* HAVE_FCHOWN */
3472
Larry Hastings2f936352014-08-05 14:04:04 +10003473
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003474#ifdef HAVE_LCHOWN
Larry Hastings2f936352014-08-05 14:04:04 +10003475/*[clinic input]
3476os.lchown
3477
3478 path : path_t
3479 uid: uid_t
3480 gid: gid_t
3481
3482Change the owner and group id of path to the numeric uid and gid.
3483
3484This function will not follow symbolic links.
3485Equivalent to os.chown(path, uid, gid, follow_symlinks=False).
3486[clinic start generated code]*/
3487
Larry Hastings2f936352014-08-05 14:04:04 +10003488static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003489os_lchown_impl(PyObject *module, path_t *path, uid_t uid, gid_t gid)
3490/*[clinic end generated code: output=25eaf6af412fdf2f input=b1c6014d563a7161]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003491{
Victor Stinner8c62be82010-05-06 00:08:46 +00003492 int res;
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003493 if (PySys_Audit("os.chown", "OIIi", path->object, uid, gid, -1) < 0) {
3494 return NULL;
3495 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003496 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10003497 res = lchown(path->narrow, uid, gid);
Victor Stinner8c62be82010-05-06 00:08:46 +00003498 Py_END_ALLOW_THREADS
Victor Stinner292c8352012-10-30 02:17:38 +01003499 if (res < 0) {
Larry Hastings2f936352014-08-05 14:04:04 +10003500 return path_error(path);
Victor Stinner292c8352012-10-30 02:17:38 +01003501 }
Larry Hastings2f936352014-08-05 14:04:04 +10003502 Py_RETURN_NONE;
Martin v. Löwis0cec0ff2002-07-28 16:33:45 +00003503}
3504#endif /* HAVE_LCHOWN */
3505
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003506
Barry Warsaw53699e91996-12-10 23:23:01 +00003507static PyObject *
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003508posix_getcwd(int use_bytes)
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003509{
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00003510#ifdef MS_WINDOWS
Victor Stinner689830e2019-06-26 17:31:12 +02003511 wchar_t wbuf[MAXPATHLEN];
3512 wchar_t *wbuf2 = wbuf;
3513 DWORD len;
3514
3515 Py_BEGIN_ALLOW_THREADS
3516 len = GetCurrentDirectoryW(Py_ARRAY_LENGTH(wbuf), wbuf);
3517 /* If the buffer is large enough, len does not include the
3518 terminating \0. If the buffer is too small, len includes
3519 the space needed for the terminator. */
3520 if (len >= Py_ARRAY_LENGTH(wbuf)) {
Victor Stinnerec3e20a2019-06-28 18:01:59 +02003521 if (len <= PY_SSIZE_T_MAX / sizeof(wchar_t)) {
Victor Stinnerb6404912013-07-07 16:21:41 +02003522 wbuf2 = PyMem_RawMalloc(len * sizeof(wchar_t));
Victor Stinner8c62be82010-05-06 00:08:46 +00003523 }
Victor Stinner689830e2019-06-26 17:31:12 +02003524 else {
3525 wbuf2 = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003526 }
Victor Stinner689830e2019-06-26 17:31:12 +02003527 if (wbuf2) {
3528 len = GetCurrentDirectoryW(len, wbuf2);
Victor Stinner8c62be82010-05-06 00:08:46 +00003529 }
Victor Stinner689830e2019-06-26 17:31:12 +02003530 }
3531 Py_END_ALLOW_THREADS
3532
3533 if (!wbuf2) {
3534 PyErr_NoMemory();
3535 return NULL;
3536 }
3537 if (!len) {
Victor Stinnerb024e842012-10-31 22:24:06 +01003538 if (wbuf2 != wbuf)
Victor Stinnerb6404912013-07-07 16:21:41 +02003539 PyMem_RawFree(wbuf2);
Victor Stinner689830e2019-06-26 17:31:12 +02003540 return PyErr_SetFromWindowsErr(0);
Victor Stinner8c62be82010-05-06 00:08:46 +00003541 }
Victor Stinnerf7c5ae22011-11-16 23:43:07 +01003542
Victor Stinner689830e2019-06-26 17:31:12 +02003543 PyObject *resobj = PyUnicode_FromWideChar(wbuf2, len);
3544 if (wbuf2 != wbuf) {
3545 PyMem_RawFree(wbuf2);
3546 }
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003547
Victor Stinner689830e2019-06-26 17:31:12 +02003548 if (use_bytes) {
3549 if (resobj == NULL) {
3550 return NULL;
3551 }
3552 Py_SETREF(resobj, PyUnicode_EncodeFSDefault(resobj));
3553 }
3554
3555 return resobj;
3556#else
3557 const size_t chunk = 1024;
3558
3559 char *buf = NULL;
3560 char *cwd = NULL;
3561 size_t buflen = 0;
3562
Victor Stinner8c62be82010-05-06 00:08:46 +00003563 Py_BEGIN_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003564 do {
Victor Stinner689830e2019-06-26 17:31:12 +02003565 char *newbuf;
3566 if (buflen <= PY_SSIZE_T_MAX - chunk) {
3567 buflen += chunk;
3568 newbuf = PyMem_RawRealloc(buf, buflen);
3569 }
3570 else {
3571 newbuf = NULL;
3572 }
3573 if (newbuf == NULL) {
3574 PyMem_RawFree(buf);
3575 buf = NULL;
Victor Stinnerc44f7072016-03-14 18:07:53 +01003576 break;
3577 }
Victor Stinner689830e2019-06-26 17:31:12 +02003578 buf = newbuf;
Victor Stinner4403d7d2015-04-25 00:16:10 +02003579
Victor Stinner4403d7d2015-04-25 00:16:10 +02003580 cwd = getcwd(buf, buflen);
3581 } while (cwd == NULL && errno == ERANGE);
Victor Stinner8c62be82010-05-06 00:08:46 +00003582 Py_END_ALLOW_THREADS
Victor Stinner4403d7d2015-04-25 00:16:10 +02003583
Victor Stinner689830e2019-06-26 17:31:12 +02003584 if (buf == NULL) {
3585 return PyErr_NoMemory();
3586 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003587 if (cwd == NULL) {
3588 PyMem_RawFree(buf);
Victor Stinner8c62be82010-05-06 00:08:46 +00003589 return posix_error();
Victor Stinner4403d7d2015-04-25 00:16:10 +02003590 }
3591
Victor Stinner689830e2019-06-26 17:31:12 +02003592 PyObject *obj;
3593 if (use_bytes) {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003594 obj = PyBytes_FromStringAndSize(buf, strlen(buf));
Victor Stinner689830e2019-06-26 17:31:12 +02003595 }
3596 else {
Victor Stinner4403d7d2015-04-25 00:16:10 +02003597 obj = PyUnicode_DecodeFSDefault(buf);
Victor Stinner689830e2019-06-26 17:31:12 +02003598 }
Victor Stinner4403d7d2015-04-25 00:16:10 +02003599 PyMem_RawFree(buf);
3600
3601 return obj;
Victor Stinner689830e2019-06-26 17:31:12 +02003602#endif /* !MS_WINDOWS */
Mark Hammondc2e85bd2002-10-03 05:10:39 +00003603}
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003604
Larry Hastings2f936352014-08-05 14:04:04 +10003605
3606/*[clinic input]
3607os.getcwd
3608
3609Return a unicode string representing the current working directory.
3610[clinic start generated code]*/
3611
Larry Hastings2f936352014-08-05 14:04:04 +10003612static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003613os_getcwd_impl(PyObject *module)
3614/*[clinic end generated code: output=21badfae2ea99ddc input=f069211bb70e3d39]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003615{
3616 return posix_getcwd(0);
3617}
3618
Larry Hastings2f936352014-08-05 14:04:04 +10003619
3620/*[clinic input]
3621os.getcwdb
3622
3623Return a bytes string representing the current working directory.
3624[clinic start generated code]*/
3625
Larry Hastings2f936352014-08-05 14:04:04 +10003626static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003627os_getcwdb_impl(PyObject *module)
3628/*[clinic end generated code: output=3dd47909480e4824 input=f6f6a378dad3d9cb]*/
Guido van Rossumf0af3e32008-10-02 18:55:37 +00003629{
3630 return posix_getcwd(1);
3631}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003632
Larry Hastings2f936352014-08-05 14:04:04 +10003633
Larry Hastings9cf065c2012-06-22 16:30:09 -07003634#if ((!defined(HAVE_LINK)) && defined(MS_WINDOWS))
3635#define HAVE_LINK 1
3636#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00003637
Guido van Rossumb6775db1994-08-01 11:34:53 +00003638#ifdef HAVE_LINK
Larry Hastings2f936352014-08-05 14:04:04 +10003639/*[clinic input]
3640
3641os.link
3642
3643 src : path_t
3644 dst : path_t
3645 *
3646 src_dir_fd : dir_fd = None
3647 dst_dir_fd : dir_fd = None
3648 follow_symlinks: bool = True
3649
3650Create a hard link to a file.
3651
3652If either src_dir_fd or dst_dir_fd is not None, it should be a file
3653 descriptor open to a directory, and the respective path string (src or dst)
3654 should be relative; the path will then be relative to that directory.
3655If follow_symlinks is False, and the last element of src is a symbolic
3656 link, link will create a link to the symbolic link itself instead of the
3657 file the link points to.
3658src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your
3659 platform. If they are unavailable, using them will raise a
3660 NotImplementedError.
3661[clinic start generated code]*/
3662
Larry Hastings2f936352014-08-05 14:04:04 +10003663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003664os_link_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04003665 int dst_dir_fd, int follow_symlinks)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003666/*[clinic end generated code: output=7f00f6007fd5269a input=b0095ebbcbaa7e04]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003667{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003668#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07003669 BOOL result = FALSE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003670#else
3671 int result;
3672#endif
3673
Larry Hastings9cf065c2012-06-22 16:30:09 -07003674#ifndef HAVE_LINKAT
3675 if ((src_dir_fd != DEFAULT_DIR_FD) || (dst_dir_fd != DEFAULT_DIR_FD)) {
3676 argument_unavailable_error("link", "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10003677 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003678 }
3679#endif
3680
Steve Dowercc16be82016-09-08 10:35:16 -07003681#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10003682 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003683 PyErr_SetString(PyExc_NotImplementedError,
3684 "link: src and dst must be the same type");
Larry Hastings2f936352014-08-05 14:04:04 +10003685 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003686 }
Steve Dowercc16be82016-09-08 10:35:16 -07003687#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003688
Saiyang Gou7514f4f2020-02-12 23:47:42 -08003689 if (PySys_Audit("os.link", "OOii", src->object, dst->object,
3690 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
3691 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
3692 return NULL;
3693 }
3694
Brian Curtin1b9df392010-11-24 20:24:31 +00003695#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07003696 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08003697 result = CreateHardLinkW(dst->wide, src->wide, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003698 Py_END_ALLOW_THREADS
Brian Curtin1b9df392010-11-24 20:24:31 +00003699
Larry Hastings2f936352014-08-05 14:04:04 +10003700 if (!result)
3701 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003702#else
3703 Py_BEGIN_ALLOW_THREADS
Larry Hastings67cbf7b2012-06-22 17:06:48 -07003704#ifdef HAVE_LINKAT
Larry Hastings9cf065c2012-06-22 16:30:09 -07003705 if ((src_dir_fd != DEFAULT_DIR_FD) ||
3706 (dst_dir_fd != DEFAULT_DIR_FD) ||
3707 (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10003708 result = linkat(src_dir_fd, src->narrow,
3709 dst_dir_fd, dst->narrow,
Larry Hastings9cf065c2012-06-22 16:30:09 -07003710 follow_symlinks ? AT_SYMLINK_FOLLOW : 0);
3711 else
Steve Dowercc16be82016-09-08 10:35:16 -07003712#endif /* HAVE_LINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10003713 result = link(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003714 Py_END_ALLOW_THREADS
Brian Curtinfc889c42010-11-28 23:59:46 +00003715
Larry Hastings2f936352014-08-05 14:04:04 +10003716 if (result)
3717 return path_error2(src, dst);
Steve Dowercc16be82016-09-08 10:35:16 -07003718#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003719
Larry Hastings2f936352014-08-05 14:04:04 +10003720 Py_RETURN_NONE;
Brian Curtin1b9df392010-11-24 20:24:31 +00003721}
Larry Hastings9cf065c2012-06-22 16:30:09 -07003722#endif
3723
Brian Curtin1b9df392010-11-24 20:24:31 +00003724
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003725#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
Barry Warsaw53699e91996-12-10 23:23:01 +00003726static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003727_listdir_windows_no_opendir(path_t *path, PyObject *list)
Guido van Rossumb6775db1994-08-01 11:34:53 +00003728{
Larry Hastings9cf065c2012-06-22 16:30:09 -07003729 PyObject *v;
3730 HANDLE hFindFile = INVALID_HANDLE_VALUE;
3731 BOOL result;
Steve Dowercc16be82016-09-08 10:35:16 -07003732 wchar_t namebuf[MAX_PATH+4]; /* Overallocate for "\*.*" */
Larry Hastings9cf065c2012-06-22 16:30:09 -07003733 /* only claim to have space for MAX_PATH */
Victor Stinner75875072013-11-24 19:23:25 +01003734 Py_ssize_t len = Py_ARRAY_LENGTH(namebuf)-4;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003735 wchar_t *wnamebuf = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003736
Steve Dowercc16be82016-09-08 10:35:16 -07003737 WIN32_FIND_DATAW wFileData;
3738 const wchar_t *po_wchars;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00003739
Steve Dowercc16be82016-09-08 10:35:16 -07003740 if (!path->wide) { /* Default arg: "." */
3741 po_wchars = L".";
3742 len = 1;
3743 } else {
3744 po_wchars = path->wide;
3745 len = wcslen(path->wide);
3746 }
3747 /* The +5 is so we can append "\\*.*\0" */
3748 wnamebuf = PyMem_New(wchar_t, len + 5);
3749 if (!wnamebuf) {
3750 PyErr_NoMemory();
Larry Hastings9cf065c2012-06-22 16:30:09 -07003751 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003752 }
Steve Dowercc16be82016-09-08 10:35:16 -07003753 wcscpy(wnamebuf, po_wchars);
Victor Stinner8c62be82010-05-06 00:08:46 +00003754 if (len > 0) {
Steve Dowercc16be82016-09-08 10:35:16 -07003755 wchar_t wch = wnamebuf[len-1];
3756 if (wch != SEP && wch != ALTSEP && wch != L':')
3757 wnamebuf[len++] = SEP;
3758 wcscpy(wnamebuf + len, L"*.*");
Victor Stinner8c62be82010-05-06 00:08:46 +00003759 }
Steve Dowercc16be82016-09-08 10:35:16 -07003760 if ((list = PyList_New(0)) == NULL) {
3761 goto exit;
3762 }
Antoine Pitroub73caab2010-08-09 23:39:31 +00003763 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003764 hFindFile = FindFirstFileW(wnamebuf, &wFileData);
Antoine Pitroub73caab2010-08-09 23:39:31 +00003765 Py_END_ALLOW_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +00003766 if (hFindFile == INVALID_HANDLE_VALUE) {
3767 int error = GetLastError();
3768 if (error == ERROR_FILE_NOT_FOUND)
Larry Hastings9cf065c2012-06-22 16:30:09 -07003769 goto exit;
3770 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003771 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003772 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003773 }
3774 do {
3775 /* Skip over . and .. */
Steve Dowercc16be82016-09-08 10:35:16 -07003776 if (wcscmp(wFileData.cFileName, L".") != 0 &&
3777 wcscmp(wFileData.cFileName, L"..") != 0) {
3778 v = PyUnicode_FromWideChar(wFileData.cFileName,
3779 wcslen(wFileData.cFileName));
3780 if (path->narrow && v) {
3781 Py_SETREF(v, PyUnicode_EncodeFSDefault(v));
3782 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003783 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003784 Py_DECREF(list);
3785 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003786 break;
3787 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003788 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003789 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003790 Py_DECREF(list);
3791 list = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00003792 break;
3793 }
3794 Py_DECREF(v);
3795 }
3796 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07003797 result = FindNextFileW(hFindFile, &wFileData);
Victor Stinner8c62be82010-05-06 00:08:46 +00003798 Py_END_ALLOW_THREADS
3799 /* FindNextFile sets error to ERROR_NO_MORE_FILES if
3800 it got to the end of the directory. */
3801 if (!result && GetLastError() != ERROR_NO_MORE_FILES) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003802 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003803 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003804 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003805 }
3806 } while (result == TRUE);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003807
Larry Hastings9cf065c2012-06-22 16:30:09 -07003808exit:
3809 if (hFindFile != INVALID_HANDLE_VALUE) {
3810 if (FindClose(hFindFile) == FALSE) {
3811 if (list != NULL) {
3812 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003813 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003814 }
3815 }
Victor Stinner8c62be82010-05-06 00:08:46 +00003816 }
Victor Stinnerb6404912013-07-07 16:21:41 +02003817 PyMem_Free(wnamebuf);
Guido van Rossumb6775db1994-08-01 11:34:53 +00003818
Larry Hastings9cf065c2012-06-22 16:30:09 -07003819 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003820} /* end of _listdir_windows_no_opendir */
3821
3822#else /* thus POSIX, ie: not (MS_WINDOWS and not HAVE_OPENDIR) */
3823
3824static PyObject *
Gregory P. Smith40a21602013-03-20 20:52:50 -07003825_posix_listdir(path_t *path, PyObject *list)
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003826{
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003827 PyObject *v;
3828 DIR *dirp = NULL;
3829 struct dirent *ep;
3830 int return_str; /* if false, return bytes */
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003831#ifdef HAVE_FDOPENDIR
3832 int fd = -1;
3833#endif
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003834
Victor Stinner8c62be82010-05-06 00:08:46 +00003835 errno = 0;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003836#ifdef HAVE_FDOPENDIR
Gregory P. Smith40a21602013-03-20 20:52:50 -07003837 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003838 /* closedir() closes the FD, so we duplicate it */
Victor Stinnerdaf45552013-08-28 00:53:59 +02003839 fd = _Py_dup(path->fd);
Victor Stinnerf3266652013-12-19 13:24:49 +01003840 if (fd == -1)
3841 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07003842
Larry Hastingsfdaea062012-06-25 04:42:23 -07003843 return_str = 1;
3844
Larry Hastings9cf065c2012-06-22 16:30:09 -07003845 Py_BEGIN_ALLOW_THREADS
3846 dirp = fdopendir(fd);
3847 Py_END_ALLOW_THREADS
3848 }
3849 else
3850#endif
3851 {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03003852 const char *name;
Gregory P. Smith40a21602013-03-20 20:52:50 -07003853 if (path->narrow) {
3854 name = path->narrow;
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +03003855 /* only return bytes if they specified a bytes-like object */
3856 return_str = !PyObject_CheckBuffer(path->object);
Larry Hastingsfdaea062012-06-25 04:42:23 -07003857 }
3858 else {
3859 name = ".";
3860 return_str = 1;
3861 }
3862
Larry Hastings9cf065c2012-06-22 16:30:09 -07003863 Py_BEGIN_ALLOW_THREADS
3864 dirp = opendir(name);
3865 Py_END_ALLOW_THREADS
3866 }
3867
3868 if (dirp == NULL) {
Gregory P. Smith40a21602013-03-20 20:52:50 -07003869 list = path_error(path);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003870#ifdef HAVE_FDOPENDIR
3871 if (fd != -1) {
3872 Py_BEGIN_ALLOW_THREADS
3873 close(fd);
3874 Py_END_ALLOW_THREADS
3875 }
3876#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003877 goto exit;
3878 }
3879 if ((list = PyList_New(0)) == NULL) {
3880 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003881 }
3882 for (;;) {
3883 errno = 0;
3884 Py_BEGIN_ALLOW_THREADS
3885 ep = readdir(dirp);
3886 Py_END_ALLOW_THREADS
3887 if (ep == NULL) {
3888 if (errno == 0) {
3889 break;
3890 } else {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003891 Py_DECREF(list);
Gregory P. Smith40a21602013-03-20 20:52:50 -07003892 list = path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003893 goto exit;
Victor Stinner8c62be82010-05-06 00:08:46 +00003894 }
3895 }
3896 if (ep->d_name[0] == '.' &&
3897 (NAMLEN(ep) == 1 ||
3898 (ep->d_name[1] == '.' && NAMLEN(ep) == 2)))
3899 continue;
Larry Hastingsfdaea062012-06-25 04:42:23 -07003900 if (return_str)
Victor Stinnera45598a2010-05-14 16:35:39 +00003901 v = PyUnicode_DecodeFSDefaultAndSize(ep->d_name, NAMLEN(ep));
3902 else
3903 v = PyBytes_FromStringAndSize(ep->d_name, NAMLEN(ep));
Victor Stinner8c62be82010-05-06 00:08:46 +00003904 if (v == NULL) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07003905 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003906 break;
3907 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07003908 if (PyList_Append(list, v) != 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00003909 Py_DECREF(v);
Larry Hastings9cf065c2012-06-22 16:30:09 -07003910 Py_CLEAR(list);
Victor Stinner8c62be82010-05-06 00:08:46 +00003911 break;
3912 }
3913 Py_DECREF(v);
3914 }
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00003915
Larry Hastings9cf065c2012-06-22 16:30:09 -07003916exit:
3917 if (dirp != NULL) {
3918 Py_BEGIN_ALLOW_THREADS
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003919#ifdef HAVE_FDOPENDIR
Larry Hastings9cf065c2012-06-22 16:30:09 -07003920 if (fd > -1)
3921 rewinddir(dirp);
Larry Hastings4dbc95e2013-08-01 18:18:56 -07003922#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07003923 closedir(dirp);
3924 Py_END_ALLOW_THREADS
3925 }
3926
Larry Hastings9cf065c2012-06-22 16:30:09 -07003927 return list;
Gregory P. Smith16ea14a2013-03-20 18:51:33 -07003928} /* end of _posix_listdir */
3929#endif /* which OS */
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00003930
Larry Hastings2f936352014-08-05 14:04:04 +10003931
3932/*[clinic input]
3933os.listdir
3934
3935 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
3936
3937Return a list containing the names of the files in the directory.
3938
BNMetricsb9427072018-11-02 15:20:19 +00003939path can be specified as either str, bytes, or a path-like object. If path is bytes,
Larry Hastings2f936352014-08-05 14:04:04 +10003940 the filenames returned will also be bytes; in all other circumstances
3941 the filenames returned will be str.
3942If path is None, uses the path='.'.
3943On some platforms, path may also be specified as an open file descriptor;\
3944 the file descriptor must refer to a directory.
3945 If this functionality is unavailable, using it raises NotImplementedError.
3946
3947The list is in arbitrary order. It does not include the special
3948entries '.' and '..' even if they are present in the directory.
3949
3950
3951[clinic start generated code]*/
3952
Larry Hastings2f936352014-08-05 14:04:04 +10003953static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003954os_listdir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +00003955/*[clinic end generated code: output=293045673fcd1a75 input=e3f58030f538295d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10003956{
Steve Dower60419a72019-06-24 08:42:54 -07003957 if (PySys_Audit("os.listdir", "O",
3958 path->object ? path->object : Py_None) < 0) {
3959 return NULL;
3960 }
Larry Hastings2f936352014-08-05 14:04:04 +10003961#if defined(MS_WINDOWS) && !defined(HAVE_OPENDIR)
3962 return _listdir_windows_no_opendir(path, NULL);
3963#else
3964 return _posix_listdir(path, NULL);
3965#endif
3966}
3967
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00003968#ifdef MS_WINDOWS
Mark Hammondef8b6542001-05-13 08:04:26 +00003969/* A helper function for abspath on win32 */
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003970/*[clinic input]
3971os._getfullpathname
Victor Stinnereb5657a2011-09-30 01:44:27 +02003972
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003973 path: path_t
3974 /
3975
3976[clinic start generated code]*/
3977
3978static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03003979os__getfullpathname_impl(PyObject *module, path_t *path)
3980/*[clinic end generated code: output=bb8679d56845bc9b input=332ed537c29d0a3e]*/
Serhiy Storchakaf0b50152015-05-13 00:52:39 +03003981{
Victor Stinner3939c322019-06-25 15:02:43 +02003982 wchar_t *abspath;
Victor Stinnereb5657a2011-09-30 01:44:27 +02003983
Victor Stinner3939c322019-06-25 15:02:43 +02003984 /* _Py_abspath() is implemented with GetFullPathNameW() on Windows */
3985 if (_Py_abspath(path->wide, &abspath) < 0) {
3986 return win32_error_object("GetFullPathNameW", path->object);
Victor Stinner8c62be82010-05-06 00:08:46 +00003987 }
Victor Stinner3939c322019-06-25 15:02:43 +02003988 if (abspath == NULL) {
3989 return PyErr_NoMemory();
3990 }
3991
3992 PyObject *str = PyUnicode_FromWideChar(abspath, wcslen(abspath));
3993 PyMem_RawFree(abspath);
3994 if (str == NULL) {
3995 return NULL;
3996 }
3997 if (path->narrow) {
3998 Py_SETREF(str, PyUnicode_EncodeFSDefault(str));
3999 }
4000 return str;
Larry Hastings2f936352014-08-05 14:04:04 +10004001}
Brian Curtind40e6f72010-07-08 21:39:08 +00004002
Brian Curtind25aef52011-06-13 15:16:04 -05004003
Larry Hastings2f936352014-08-05 14:04:04 +10004004/*[clinic input]
4005os._getfinalpathname
Brian Curtinf5e76d02010-11-24 13:14:05 +00004006
Steve Dower23ad6d02018-02-22 10:39:10 -08004007 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004008 /
4009
4010A helper function for samepath on windows.
4011[clinic start generated code]*/
4012
Larry Hastings2f936352014-08-05 14:04:04 +10004013static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004014os__getfinalpathname_impl(PyObject *module, path_t *path)
4015/*[clinic end generated code: output=621a3c79bc29ebfa input=2b6b6c7cbad5fb84]*/
Brian Curtind40e6f72010-07-08 21:39:08 +00004016{
4017 HANDLE hFile;
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004018 wchar_t buf[MAXPATHLEN], *target_path = buf;
4019 int buf_size = Py_ARRAY_LENGTH(buf);
Brian Curtind40e6f72010-07-08 21:39:08 +00004020 int result_length;
Larry Hastings2f936352014-08-05 14:04:04 +10004021 PyObject *result;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004022
Steve Dower23ad6d02018-02-22 10:39:10 -08004023 Py_BEGIN_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004024 hFile = CreateFileW(
Steve Dower23ad6d02018-02-22 10:39:10 -08004025 path->wide,
Brian Curtind40e6f72010-07-08 21:39:08 +00004026 0, /* desired access */
4027 0, /* share mode */
4028 NULL, /* security attributes */
4029 OPEN_EXISTING,
4030 /* FILE_FLAG_BACKUP_SEMANTICS is required to open a directory */
4031 FILE_FLAG_BACKUP_SEMANTICS,
4032 NULL);
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004033 Py_END_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004034
Steve Dower23ad6d02018-02-22 10:39:10 -08004035 if (hFile == INVALID_HANDLE_VALUE) {
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004036 return win32_error_object("CreateFileW", path->object);
Steve Dower23ad6d02018-02-22 10:39:10 -08004037 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004038
4039 /* We have a good handle to the target, use it to determine the
4040 target path name. */
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004041 while (1) {
4042 Py_BEGIN_ALLOW_THREADS
4043 result_length = GetFinalPathNameByHandleW(hFile, target_path,
4044 buf_size, VOLUME_NAME_DOS);
4045 Py_END_ALLOW_THREADS
Brian Curtind40e6f72010-07-08 21:39:08 +00004046
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004047 if (!result_length) {
4048 result = win32_error_object("GetFinalPathNameByHandleW",
4049 path->object);
4050 goto cleanup;
4051 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004052
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004053 if (result_length < buf_size) {
4054 break;
4055 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004056
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004057 wchar_t *tmp;
4058 tmp = PyMem_Realloc(target_path != buf ? target_path : NULL,
4059 result_length * sizeof(*tmp));
4060 if (!tmp) {
4061 result = PyErr_NoMemory();
4062 goto cleanup;
4063 }
4064
4065 buf_size = result_length;
4066 target_path = tmp;
Steve Dower23ad6d02018-02-22 10:39:10 -08004067 }
Brian Curtind40e6f72010-07-08 21:39:08 +00004068
Victor Stinner9d3b93b2011-11-22 02:27:30 +01004069 result = PyUnicode_FromWideChar(target_path, result_length);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004070 if (result && path->narrow) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004071 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Steve Dowerdf2d4a62019-08-21 15:27:33 -07004072 }
Steve Dower23ad6d02018-02-22 10:39:10 -08004073
Alexey Izbyshev3b20d342018-03-08 19:03:25 +03004074cleanup:
4075 if (target_path != buf) {
4076 PyMem_Free(target_path);
4077 }
4078 CloseHandle(hFile);
4079 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10004080}
Brian Curtin62857742010-09-06 17:07:27 +00004081
Tim Golden6b528062013-08-01 12:44:00 +01004082
Larry Hastings2f936352014-08-05 14:04:04 +10004083/*[clinic input]
4084os._getvolumepathname
4085
Steve Dower23ad6d02018-02-22 10:39:10 -08004086 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10004087
4088A helper function for ismount on Win32.
4089[clinic start generated code]*/
4090
Larry Hastings2f936352014-08-05 14:04:04 +10004091static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -08004092os__getvolumepathname_impl(PyObject *module, path_t *path)
4093/*[clinic end generated code: output=804c63fd13a1330b input=722b40565fa21552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004094{
4095 PyObject *result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004096 wchar_t *mountpath=NULL;
Victor Stinner6edddfa2013-11-24 19:22:57 +01004097 size_t buflen;
Tim Golden6b528062013-08-01 12:44:00 +01004098 BOOL ret;
4099
Tim Golden6b528062013-08-01 12:44:00 +01004100 /* Volume path should be shorter than entire path */
Steve Dower23ad6d02018-02-22 10:39:10 -08004101 buflen = Py_MAX(path->length, MAX_PATH);
Victor Stinner6edddfa2013-11-24 19:22:57 +01004102
Victor Stinner850a18e2017-10-24 16:53:32 -07004103 if (buflen > PY_DWORD_MAX) {
Victor Stinner6edddfa2013-11-24 19:22:57 +01004104 PyErr_SetString(PyExc_OverflowError, "path too long");
4105 return NULL;
4106 }
4107
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02004108 mountpath = PyMem_New(wchar_t, buflen);
Tim Golden6b528062013-08-01 12:44:00 +01004109 if (mountpath == NULL)
4110 return PyErr_NoMemory();
4111
4112 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -08004113 ret = GetVolumePathNameW(path->wide, mountpath,
Victor Stinner6edddfa2013-11-24 19:22:57 +01004114 Py_SAFE_DOWNCAST(buflen, size_t, DWORD));
Tim Golden6b528062013-08-01 12:44:00 +01004115 Py_END_ALLOW_THREADS
4116
4117 if (!ret) {
Steve Dower23ad6d02018-02-22 10:39:10 -08004118 result = win32_error_object("_getvolumepathname", path->object);
Tim Golden6b528062013-08-01 12:44:00 +01004119 goto exit;
4120 }
4121 result = PyUnicode_FromWideChar(mountpath, wcslen(mountpath));
Steve Dower23ad6d02018-02-22 10:39:10 -08004122 if (path->narrow)
4123 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
Tim Golden6b528062013-08-01 12:44:00 +01004124
4125exit:
4126 PyMem_Free(mountpath);
4127 return result;
4128}
Tim Golden6b528062013-08-01 12:44:00 +01004129
Martin v. Löwis6238d2b2002-06-30 15:26:10 +00004130#endif /* MS_WINDOWS */
Mark Hammondef8b6542001-05-13 08:04:26 +00004131
Larry Hastings2f936352014-08-05 14:04:04 +10004132
4133/*[clinic input]
4134os.mkdir
4135
4136 path : path_t
4137
4138 mode: int = 0o777
4139
4140 *
4141
4142 dir_fd : dir_fd(requires='mkdirat') = None
4143
4144# "mkdir(path, mode=0o777, *, dir_fd=None)\n\n\
4145
4146Create a directory.
4147
4148If dir_fd is not None, it should be a file descriptor open to a directory,
4149 and path should be relative; path will then be relative to that directory.
4150dir_fd may not be implemented on your platform.
4151 If it is unavailable, using it will raise a NotImplementedError.
4152
4153The mode argument is ignored on Windows.
4154[clinic start generated code]*/
4155
Larry Hastings2f936352014-08-05 14:04:04 +10004156static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004157os_mkdir_impl(PyObject *module, path_t *path, int mode, int dir_fd)
4158/*[clinic end generated code: output=a70446903abe821f input=e965f68377e9b1ce]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004159{
4160 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004161
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004162 if (PySys_Audit("os.mkdir", "Oii", path->object, mode,
4163 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4164 return NULL;
4165 }
4166
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00004167#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004168 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004169 result = CreateDirectoryW(path->wide, NULL);
Victor Stinner8c62be82010-05-06 00:08:46 +00004170 Py_END_ALLOW_THREADS
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004171
Larry Hastings2f936352014-08-05 14:04:04 +10004172 if (!result)
4173 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004174#else
Victor Stinner8c62be82010-05-06 00:08:46 +00004175 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004176#if HAVE_MKDIRAT
4177 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004178 result = mkdirat(dir_fd, path->narrow, mode);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004179 else
4180#endif
Erik Bray03eb11f2017-10-27 14:27:06 +02004181#if defined(__WATCOMC__) && !defined(__QNX__)
Larry Hastings2f936352014-08-05 14:04:04 +10004182 result = mkdir(path->narrow);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004183#else
Larry Hastings2f936352014-08-05 14:04:04 +10004184 result = mkdir(path->narrow, mode);
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004185#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004186 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004187 if (result < 0)
4188 return path_error(path);
Steve Dowercc16be82016-09-08 10:35:16 -07004189#endif /* MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +10004190 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004191}
4192
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004193
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004194/* sys/resource.h is needed for at least: wait3(), wait4(), broken nice. */
4195#if defined(HAVE_SYS_RESOURCE_H)
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004196#include <sys/resource.h>
4197#endif
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004198
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004199
4200#ifdef HAVE_NICE
Larry Hastings2f936352014-08-05 14:04:04 +10004201/*[clinic input]
4202os.nice
4203
4204 increment: int
4205 /
4206
4207Add increment to the priority of process and return the new priority.
4208[clinic start generated code]*/
4209
Larry Hastings2f936352014-08-05 14:04:04 +10004210static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004211os_nice_impl(PyObject *module, int increment)
4212/*[clinic end generated code: output=9dad8a9da8109943 input=864be2d402a21da2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004213{
4214 int value;
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004215
Victor Stinner8c62be82010-05-06 00:08:46 +00004216 /* There are two flavours of 'nice': one that returns the new
4217 priority (as required by almost all standards out there) and the
Benjamin Peterson288d1da2017-09-28 22:44:27 -07004218 Linux/FreeBSD one, which returns '0' on success and advices
Victor Stinner8c62be82010-05-06 00:08:46 +00004219 the use of getpriority() to get the new priority.
Tim Peters5aa91602002-01-30 05:46:57 +00004220
Victor Stinner8c62be82010-05-06 00:08:46 +00004221 If we are of the nice family that returns the new priority, we
4222 need to clear errno before the call, and check if errno is filled
4223 before calling posix_error() on a returnvalue of -1, because the
4224 -1 may be the actual new priority! */
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004225
Victor Stinner8c62be82010-05-06 00:08:46 +00004226 errno = 0;
4227 value = nice(increment);
Thomas Wouterse38b2f12001-07-11 22:35:31 +00004228#if defined(HAVE_BROKEN_NICE) && defined(HAVE_GETPRIORITY)
Victor Stinner8c62be82010-05-06 00:08:46 +00004229 if (value == 0)
4230 value = getpriority(PRIO_PROCESS, 0);
Thomas Woutersc2c12dc2001-07-11 14:45:34 +00004231#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00004232 if (value == -1 && errno != 0)
4233 /* either nice() or getpriority() returned an error */
4234 return posix_error();
4235 return PyLong_FromLong((long) value);
Guido van Rossum775f4da1993-01-09 17:18:52 +00004236}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004237#endif /* HAVE_NICE */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004238
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004239
4240#ifdef HAVE_GETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004241/*[clinic input]
4242os.getpriority
4243
4244 which: int
4245 who: int
4246
4247Return program scheduling priority.
4248[clinic start generated code]*/
4249
Larry Hastings2f936352014-08-05 14:04:04 +10004250static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004251os_getpriority_impl(PyObject *module, int which, int who)
4252/*[clinic end generated code: output=c41b7b63c7420228 input=9be615d40e2544ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004253{
4254 int retval;
4255
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004256 errno = 0;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004257 retval = getpriority(which, who);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004258 if (errno != 0)
4259 return posix_error();
4260 return PyLong_FromLong((long)retval);
4261}
4262#endif /* HAVE_GETPRIORITY */
4263
4264
4265#ifdef HAVE_SETPRIORITY
Larry Hastings2f936352014-08-05 14:04:04 +10004266/*[clinic input]
4267os.setpriority
4268
4269 which: int
4270 who: int
4271 priority: int
4272
4273Set program scheduling priority.
4274[clinic start generated code]*/
4275
Larry Hastings2f936352014-08-05 14:04:04 +10004276static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004277os_setpriority_impl(PyObject *module, int which, int who, int priority)
4278/*[clinic end generated code: output=3d910d95a7771eb2 input=710ccbf65b9dc513]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004279{
4280 int retval;
4281
4282 retval = setpriority(which, who, priority);
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +00004283 if (retval == -1)
4284 return posix_error();
4285 Py_RETURN_NONE;
4286}
4287#endif /* HAVE_SETPRIORITY */
4288
4289
Barry Warsaw53699e91996-12-10 23:23:01 +00004290static PyObject *
Larry Hastings2f936352014-08-05 14:04:04 +10004291internal_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 +00004292{
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004293 const char *function_name = is_replace ? "replace" : "rename";
Larry Hastings9cf065c2012-06-22 16:30:09 -07004294 int dir_fd_specified;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004295
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004296#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00004297 BOOL result;
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004298 int flags = is_replace ? MOVEFILE_REPLACE_EXISTING : 0;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004299#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004300 int result;
Mark Hammondc2e85bd2002-10-03 05:10:39 +00004301#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004302
Larry Hastings9cf065c2012-06-22 16:30:09 -07004303 dir_fd_specified = (src_dir_fd != DEFAULT_DIR_FD) ||
4304 (dst_dir_fd != DEFAULT_DIR_FD);
4305#ifndef HAVE_RENAMEAT
4306 if (dir_fd_specified) {
4307 argument_unavailable_error(function_name, "src_dir_fd and dst_dir_fd");
Larry Hastings2f936352014-08-05 14:04:04 +10004308 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004309 }
4310#endif
4311
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004312 if (PySys_Audit("os.rename", "OOii", src->object, dst->object,
4313 src_dir_fd == DEFAULT_DIR_FD ? -1 : src_dir_fd,
4314 dst_dir_fd == DEFAULT_DIR_FD ? -1 : dst_dir_fd) < 0) {
4315 return NULL;
4316 }
4317
Larry Hastings9cf065c2012-06-22 16:30:09 -07004318#ifdef MS_WINDOWS
4319 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07004320 result = MoveFileExW(src->wide, dst->wide, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004321 Py_END_ALLOW_THREADS
4322
Larry Hastings2f936352014-08-05 14:04:04 +10004323 if (!result)
4324 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004325
4326#else
Steve Dowercc16be82016-09-08 10:35:16 -07004327 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
4328 PyErr_Format(PyExc_ValueError,
4329 "%s: src and dst must be the same type", function_name);
4330 return NULL;
4331 }
4332
Larry Hastings9cf065c2012-06-22 16:30:09 -07004333 Py_BEGIN_ALLOW_THREADS
4334#ifdef HAVE_RENAMEAT
4335 if (dir_fd_specified)
Larry Hastings2f936352014-08-05 14:04:04 +10004336 result = renameat(src_dir_fd, src->narrow, dst_dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004337 else
4338#endif
Steve Dowercc16be82016-09-08 10:35:16 -07004339 result = rename(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004340 Py_END_ALLOW_THREADS
4341
Larry Hastings2f936352014-08-05 14:04:04 +10004342 if (result)
4343 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004344#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004345 Py_RETURN_NONE;
4346}
Larry Hastings9cf065c2012-06-22 16:30:09 -07004347
Larry Hastings2f936352014-08-05 14:04:04 +10004348
4349/*[clinic input]
4350os.rename
4351
4352 src : path_t
4353 dst : path_t
4354 *
4355 src_dir_fd : dir_fd = None
4356 dst_dir_fd : dir_fd = None
4357
4358Rename a file or directory.
4359
4360If either src_dir_fd or dst_dir_fd is not None, it should be a file
4361 descriptor open to a directory, and the respective path string (src or dst)
4362 should be relative; the path will then be relative to that directory.
4363src_dir_fd and dst_dir_fd, may not be implemented on your platform.
4364 If they are unavailable, using them will raise a NotImplementedError.
4365[clinic start generated code]*/
4366
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004367static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004368os_rename_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
Larry Hastings89964c42015-04-14 18:07:59 -04004369 int dst_dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004370/*[clinic end generated code: output=59e803072cf41230 input=faa61c847912c850]*/
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004371{
Larry Hastings2f936352014-08-05 14:04:04 +10004372 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 0);
Antoine Pitrouf3b2d882012-01-30 22:08:52 +01004373}
4374
Larry Hastings2f936352014-08-05 14:04:04 +10004375
4376/*[clinic input]
4377os.replace = os.rename
4378
4379Rename a file or directory, overwriting the destination.
4380
4381If either src_dir_fd or dst_dir_fd is not None, it should be a file
4382 descriptor open to a directory, and the respective path string (src or dst)
4383 should be relative; the path will then be relative to that directory.
4384src_dir_fd and dst_dir_fd, may not be implemented on your platform.
Anthony Sottile73d60022019-02-12 23:15:54 -05004385 If they are unavailable, using them will raise a NotImplementedError.
Larry Hastings2f936352014-08-05 14:04:04 +10004386[clinic start generated code]*/
4387
Larry Hastings2f936352014-08-05 14:04:04 +10004388static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004389os_replace_impl(PyObject *module, path_t *src, path_t *dst, int src_dir_fd,
4390 int dst_dir_fd)
Anthony Sottile73d60022019-02-12 23:15:54 -05004391/*[clinic end generated code: output=1968c02e7857422b input=c003f0def43378ef]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004392{
4393 return internal_rename(src, dst, src_dir_fd, dst_dir_fd, 1);
4394}
4395
4396
4397/*[clinic input]
4398os.rmdir
4399
4400 path: path_t
4401 *
4402 dir_fd: dir_fd(requires='unlinkat') = None
4403
4404Remove a directory.
4405
4406If dir_fd is not None, it should be a file descriptor open to a directory,
4407 and path should be relative; path will then be relative to that directory.
4408dir_fd may not be implemented on your platform.
4409 If it is unavailable, using it will raise a NotImplementedError.
4410[clinic start generated code]*/
4411
Larry Hastings2f936352014-08-05 14:04:04 +10004412static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004413os_rmdir_impl(PyObject *module, path_t *path, int dir_fd)
4414/*[clinic end generated code: output=080eb54f506e8301 input=38c8b375ca34a7e2]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004415{
4416 int result;
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004417
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004418 if (PySys_Audit("os.rmdir", "Oi", path->object,
4419 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4420 return NULL;
4421 }
4422
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004423 Py_BEGIN_ALLOW_THREADS
4424#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004425 /* Windows, success=1, UNIX, success=0 */
4426 result = !RemoveDirectoryW(path->wide);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004427#else
4428#ifdef HAVE_UNLINKAT
4429 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004430 result = unlinkat(dir_fd, path->narrow, AT_REMOVEDIR);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004431 else
4432#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004433 result = rmdir(path->narrow);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004434#endif
4435 Py_END_ALLOW_THREADS
4436
Larry Hastings2f936352014-08-05 14:04:04 +10004437 if (result)
4438 return path_error(path);
Larry Hastingsb698d8e2012-06-23 16:55:07 -07004439
Larry Hastings2f936352014-08-05 14:04:04 +10004440 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004441}
4442
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004443
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004444#ifdef HAVE_SYSTEM
Larry Hastings2f936352014-08-05 14:04:04 +10004445#ifdef MS_WINDOWS
4446/*[clinic input]
4447os.system -> long
4448
4449 command: Py_UNICODE
4450
4451Execute the command in a subshell.
4452[clinic start generated code]*/
4453
Larry Hastings2f936352014-08-05 14:04:04 +10004454static long
Serhiy Storchakaafb3e712018-12-14 11:19:51 +02004455os_system_impl(PyObject *module, const Py_UNICODE *command)
4456/*[clinic end generated code: output=5b7c3599c068ca42 input=303f5ce97df606b0]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004457{
4458 long result;
Steve Dowerb82e17e2019-05-23 08:45:22 -07004459
Steve Dowerfbe3c762019-10-18 00:52:15 -07004460 if (PySys_Audit("os.system", "(u)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004461 return -1;
4462 }
4463
Victor Stinner8c62be82010-05-06 00:08:46 +00004464 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08004465 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +10004466 result = _wsystem(command);
Steve Dowerc3630612016-11-19 18:41:16 -08004467 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00004468 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10004469 return result;
4470}
4471#else /* MS_WINDOWS */
4472/*[clinic input]
4473os.system -> long
4474
4475 command: FSConverter
4476
4477Execute the command in a subshell.
4478[clinic start generated code]*/
4479
Larry Hastings2f936352014-08-05 14:04:04 +10004480static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004481os_system_impl(PyObject *module, PyObject *command)
4482/*[clinic end generated code: output=290fc437dd4f33a0 input=86a58554ba6094af]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004483{
4484 long result;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03004485 const char *bytes = PyBytes_AsString(command);
Steve Dowerb82e17e2019-05-23 08:45:22 -07004486
Steve Dowerfbe3c762019-10-18 00:52:15 -07004487 if (PySys_Audit("os.system", "(O)", command) < 0) {
Steve Dowerb82e17e2019-05-23 08:45:22 -07004488 return -1;
4489 }
4490
Larry Hastings2f936352014-08-05 14:04:04 +10004491 Py_BEGIN_ALLOW_THREADS
4492 result = system(bytes);
4493 Py_END_ALLOW_THREADS
4494 return result;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004495}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00004496#endif
Larry Hastings2f936352014-08-05 14:04:04 +10004497#endif /* HAVE_SYSTEM */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004498
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004499
Larry Hastings2f936352014-08-05 14:04:04 +10004500/*[clinic input]
4501os.umask
4502
4503 mask: int
4504 /
4505
4506Set the current numeric umask and return the previous umask.
4507[clinic start generated code]*/
4508
Larry Hastings2f936352014-08-05 14:04:04 +10004509static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004510os_umask_impl(PyObject *module, int mask)
4511/*[clinic end generated code: output=a2e33ce3bc1a6e33 input=ab6bfd9b24d8a7e8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004512{
4513 int i = (int)umask(mask);
Victor Stinner8c62be82010-05-06 00:08:46 +00004514 if (i < 0)
4515 return posix_error();
4516 return PyLong_FromLong((long)i);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004517}
4518
Brian Curtind40e6f72010-07-08 21:39:08 +00004519#ifdef MS_WINDOWS
4520
4521/* override the default DeleteFileW behavior so that directory
4522symlinks can be removed with this function, the same as with
4523Unix symlinks */
4524BOOL WINAPI Py_DeleteFileW(LPCWSTR lpFileName)
4525{
4526 WIN32_FILE_ATTRIBUTE_DATA info;
4527 WIN32_FIND_DATAW find_data;
4528 HANDLE find_data_handle;
4529 int is_directory = 0;
4530 int is_link = 0;
4531
4532 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
4533 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00004534
Brian Curtind40e6f72010-07-08 21:39:08 +00004535 /* Get WIN32_FIND_DATA structure for the path to determine if
4536 it is a symlink */
4537 if(is_directory &&
4538 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
4539 find_data_handle = FindFirstFileW(lpFileName, &find_data);
4540
4541 if(find_data_handle != INVALID_HANDLE_VALUE) {
Tim Golden0321cf22014-05-05 19:46:17 +01004542 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
4543 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
4544 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
4545 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
Brian Curtind40e6f72010-07-08 21:39:08 +00004546 FindClose(find_data_handle);
4547 }
4548 }
4549 }
4550
4551 if (is_directory && is_link)
4552 return RemoveDirectoryW(lpFileName);
4553
4554 return DeleteFileW(lpFileName);
4555}
4556#endif /* MS_WINDOWS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004557
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004558
Larry Hastings2f936352014-08-05 14:04:04 +10004559/*[clinic input]
4560os.unlink
4561
4562 path: path_t
4563 *
4564 dir_fd: dir_fd(requires='unlinkat')=None
4565
4566Remove a file (same as remove()).
4567
4568If dir_fd is not None, it should be a file descriptor open to a directory,
4569 and path should be relative; path will then be relative to that directory.
4570dir_fd may not be implemented on your platform.
4571 If it is unavailable, using it will raise a NotImplementedError.
4572
4573[clinic start generated code]*/
4574
Larry Hastings2f936352014-08-05 14:04:04 +10004575static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004576os_unlink_impl(PyObject *module, path_t *path, int dir_fd)
4577/*[clinic end generated code: output=621797807b9963b1 input=d7bcde2b1b2a2552]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004578{
4579 int result;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004580
Saiyang Gou7514f4f2020-02-12 23:47:42 -08004581 if (PySys_Audit("os.remove", "Oi", path->object,
4582 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
4583 return NULL;
4584 }
4585
Larry Hastings9cf065c2012-06-22 16:30:09 -07004586 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04004587 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004588#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07004589 /* Windows, success=1, UNIX, success=0 */
4590 result = !Py_DeleteFileW(path->wide);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004591#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07004592#ifdef HAVE_UNLINKAT
4593 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10004594 result = unlinkat(dir_fd, path->narrow, 0);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004595 else
4596#endif /* HAVE_UNLINKAT */
Larry Hastings2f936352014-08-05 14:04:04 +10004597 result = unlink(path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004598#endif
Steve Dower8fc89802015-04-12 00:26:27 -04004599 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07004600 Py_END_ALLOW_THREADS
4601
Larry Hastings2f936352014-08-05 14:04:04 +10004602 if (result)
4603 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -07004604
Larry Hastings2f936352014-08-05 14:04:04 +10004605 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004606}
4607
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004608
Larry Hastings2f936352014-08-05 14:04:04 +10004609/*[clinic input]
4610os.remove = os.unlink
4611
4612Remove a file (same as unlink()).
4613
4614If dir_fd is not None, it should be a file descriptor open to a directory,
4615 and path should be relative; path will then be relative to that directory.
4616dir_fd may not be implemented on your platform.
4617 If it is unavailable, using it will raise a NotImplementedError.
4618[clinic start generated code]*/
4619
Larry Hastings2f936352014-08-05 14:04:04 +10004620static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004621os_remove_impl(PyObject *module, path_t *path, int dir_fd)
4622/*[clinic end generated code: output=a8535b28f0068883 input=e05c5ab55cd30983]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004623{
4624 return os_unlink_impl(module, path, dir_fd);
4625}
4626
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00004627
Larry Hastings605a62d2012-06-24 04:33:36 -07004628static PyStructSequence_Field uname_result_fields[] = {
4629 {"sysname", "operating system name"},
4630 {"nodename", "name of machine on network (implementation-defined)"},
4631 {"release", "operating system release"},
4632 {"version", "operating system version"},
4633 {"machine", "hardware identifier"},
4634 {NULL}
4635};
4636
4637PyDoc_STRVAR(uname_result__doc__,
4638"uname_result: Result from os.uname().\n\n\
4639This object may be accessed either as a tuple of\n\
4640 (sysname, nodename, release, version, machine),\n\
4641or via the attributes sysname, nodename, release, version, and machine.\n\
4642\n\
4643See os.uname for more information.");
4644
4645static PyStructSequence_Desc uname_result_desc = {
Eddie Elizondob3966632019-11-05 07:16:14 -08004646 MODNAME ".uname_result", /* name */
Larry Hastings605a62d2012-06-24 04:33:36 -07004647 uname_result__doc__, /* doc */
4648 uname_result_fields,
4649 5
4650};
4651
Larry Hastings605a62d2012-06-24 04:33:36 -07004652#ifdef HAVE_UNAME
Larry Hastings2f936352014-08-05 14:04:04 +10004653/*[clinic input]
4654os.uname
4655
4656Return an object identifying the current operating system.
4657
4658The object behaves like a named tuple with the following fields:
4659 (sysname, nodename, release, version, machine)
4660
4661[clinic start generated code]*/
4662
Larry Hastings2f936352014-08-05 14:04:04 +10004663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004664os_uname_impl(PyObject *module)
4665/*[clinic end generated code: output=e6a49cf1a1508a19 input=e68bd246db3043ed]*/
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004666{
Victor Stinner8c62be82010-05-06 00:08:46 +00004667 struct utsname u;
4668 int res;
Larry Hastings605a62d2012-06-24 04:33:36 -07004669 PyObject *value;
Neal Norwitze241ce82003-02-17 18:17:05 +00004670
Victor Stinner8c62be82010-05-06 00:08:46 +00004671 Py_BEGIN_ALLOW_THREADS
4672 res = uname(&u);
4673 Py_END_ALLOW_THREADS
4674 if (res < 0)
4675 return posix_error();
Larry Hastings605a62d2012-06-24 04:33:36 -07004676
Hai Shif707d942020-03-16 21:15:01 +08004677 PyObject *UnameResultType = get_posix_state(module)->UnameResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08004678 value = PyStructSequence_New((PyTypeObject *)UnameResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07004679 if (value == NULL)
4680 return NULL;
4681
4682#define SET(i, field) \
4683 { \
Victor Stinnera534fc42013-06-03 22:07:27 +02004684 PyObject *o = PyUnicode_DecodeFSDefault(field); \
Larry Hastings605a62d2012-06-24 04:33:36 -07004685 if (!o) { \
4686 Py_DECREF(value); \
4687 return NULL; \
4688 } \
4689 PyStructSequence_SET_ITEM(value, i, o); \
4690 } \
4691
4692 SET(0, u.sysname);
4693 SET(1, u.nodename);
4694 SET(2, u.release);
4695 SET(3, u.version);
4696 SET(4, u.machine);
4697
4698#undef SET
4699
4700 return value;
Guido van Rossumc39de5f1992-02-05 11:15:54 +00004701}
Guido van Rossumb6775db1994-08-01 11:34:53 +00004702#endif /* HAVE_UNAME */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00004703
Larry Hastings9e3e70b2011-09-08 19:29:07 -07004704
Larry Hastings9cf065c2012-06-22 16:30:09 -07004705
4706typedef struct {
4707 int now;
4708 time_t atime_s;
4709 long atime_ns;
4710 time_t mtime_s;
4711 long mtime_ns;
4712} utime_t;
4713
4714/*
Victor Stinner484df002014-10-09 13:52:31 +02004715 * these macros assume that "ut" is a pointer to a utime_t
Larry Hastings9cf065c2012-06-22 16:30:09 -07004716 * they also intentionally leak the declaration of a pointer named "time"
4717 */
4718#define UTIME_TO_TIMESPEC \
4719 struct timespec ts[2]; \
4720 struct timespec *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004721 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004722 time = NULL; \
4723 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004724 ts[0].tv_sec = ut->atime_s; \
4725 ts[0].tv_nsec = ut->atime_ns; \
4726 ts[1].tv_sec = ut->mtime_s; \
4727 ts[1].tv_nsec = ut->mtime_ns; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004728 time = ts; \
4729 } \
4730
4731#define UTIME_TO_TIMEVAL \
4732 struct timeval tv[2]; \
4733 struct timeval *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004734 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004735 time = NULL; \
4736 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004737 tv[0].tv_sec = ut->atime_s; \
4738 tv[0].tv_usec = ut->atime_ns / 1000; \
4739 tv[1].tv_sec = ut->mtime_s; \
4740 tv[1].tv_usec = ut->mtime_ns / 1000; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004741 time = tv; \
4742 } \
4743
4744#define UTIME_TO_UTIMBUF \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004745 struct utimbuf u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004746 struct utimbuf *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004747 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004748 time = NULL; \
4749 else { \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004750 u.actime = ut->atime_s; \
4751 u.modtime = ut->mtime_s; \
4752 time = &u; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004753 }
4754
4755#define UTIME_TO_TIME_T \
4756 time_t timet[2]; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004757 time_t *time; \
Victor Stinner484df002014-10-09 13:52:31 +02004758 if (ut->now) \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004759 time = NULL; \
4760 else { \
Victor Stinner484df002014-10-09 13:52:31 +02004761 timet[0] = ut->atime_s; \
4762 timet[1] = ut->mtime_s; \
Georg Brandle1a7d9d2014-10-12 08:45:15 +02004763 time = timet; \
Larry Hastings9cf065c2012-06-22 16:30:09 -07004764 } \
4765
4766
Victor Stinner528a9ab2015-09-03 21:30:26 +02004767#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004768
4769static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004770utime_dir_fd(utime_t *ut, int dir_fd, const char *path, int follow_symlinks)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004771{
4772#ifdef HAVE_UTIMENSAT
4773 int flags = follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW;
4774 UTIME_TO_TIMESPEC;
4775 return utimensat(dir_fd, path, time, flags);
4776#elif defined(HAVE_FUTIMESAT)
4777 UTIME_TO_TIMEVAL;
4778 /*
4779 * follow_symlinks will never be false here;
4780 * we only allow !follow_symlinks and dir_fd together
4781 * if we have utimensat()
4782 */
4783 assert(follow_symlinks);
4784 return futimesat(dir_fd, path, time);
4785#endif
4786}
4787
Larry Hastings2f936352014-08-05 14:04:04 +10004788 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_converter
4789#else
4790 #define FUTIMENSAT_DIR_FD_CONVERTER dir_fd_unavailable
Larry Hastings9cf065c2012-06-22 16:30:09 -07004791#endif
4792
Victor Stinner528a9ab2015-09-03 21:30:26 +02004793#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004794
4795static int
Victor Stinner484df002014-10-09 13:52:31 +02004796utime_fd(utime_t *ut, int fd)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004797{
4798#ifdef HAVE_FUTIMENS
4799 UTIME_TO_TIMESPEC;
4800 return futimens(fd, time);
4801#else
4802 UTIME_TO_TIMEVAL;
4803 return futimes(fd, time);
4804#endif
4805}
4806
Larry Hastings2f936352014-08-05 14:04:04 +10004807 #define PATH_UTIME_HAVE_FD 1
4808#else
4809 #define PATH_UTIME_HAVE_FD 0
Larry Hastings9cf065c2012-06-22 16:30:09 -07004810#endif
4811
Victor Stinner5ebae872015-09-22 01:29:33 +02004812#if defined(HAVE_UTIMENSAT) || defined(HAVE_LUTIMES)
4813# define UTIME_HAVE_NOFOLLOW_SYMLINKS
4814#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07004815
Victor Stinner4552ced2015-09-21 22:37:15 +02004816#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07004817
4818static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004819utime_nofollow_symlinks(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004820{
4821#ifdef HAVE_UTIMENSAT
4822 UTIME_TO_TIMESPEC;
4823 return utimensat(DEFAULT_DIR_FD, path, time, AT_SYMLINK_NOFOLLOW);
4824#else
4825 UTIME_TO_TIMEVAL;
4826 return lutimes(path, time);
4827#endif
4828}
4829
4830#endif
4831
4832#ifndef MS_WINDOWS
4833
4834static int
Serhiy Storchakaef1585e2015-12-25 20:01:53 +02004835utime_default(utime_t *ut, const char *path)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004836{
4837#ifdef HAVE_UTIMENSAT
4838 UTIME_TO_TIMESPEC;
4839 return utimensat(DEFAULT_DIR_FD, path, time, 0);
4840#elif defined(HAVE_UTIMES)
4841 UTIME_TO_TIMEVAL;
4842 return utimes(path, time);
4843#elif defined(HAVE_UTIME_H)
4844 UTIME_TO_UTIMBUF;
4845 return utime(path, time);
4846#else
4847 UTIME_TO_TIME_T;
4848 return utime(path, time);
4849#endif
4850}
4851
4852#endif
4853
Larry Hastings76ad59b2012-05-03 00:30:07 -07004854static int
4855split_py_long_to_s_and_ns(PyObject *py_long, time_t *s, long *ns)
4856{
4857 int result = 0;
Benjamin Petersonfbd85a02012-05-04 11:06:09 -04004858 PyObject *divmod;
Eddie Elizondob3966632019-11-05 07:16:14 -08004859 divmod = PyNumber_Divmod(py_long, _posixstate_global->billion);
Larry Hastings76ad59b2012-05-03 00:30:07 -07004860 if (!divmod)
4861 goto exit;
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004862 if (!PyTuple_Check(divmod) || PyTuple_GET_SIZE(divmod) != 2) {
4863 PyErr_Format(PyExc_TypeError,
4864 "%.200s.__divmod__() must return a 2-tuple, not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -08004865 _PyType_Name(Py_TYPE(py_long)), _PyType_Name(Py_TYPE(divmod)));
Oren Milman0bd1a2d2018-09-12 22:14:35 +03004866 goto exit;
4867 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07004868 *s = _PyLong_AsTime_t(PyTuple_GET_ITEM(divmod, 0));
4869 if ((*s == -1) && PyErr_Occurred())
4870 goto exit;
4871 *ns = PyLong_AsLong(PyTuple_GET_ITEM(divmod, 1));
Benjamin Peterson35a8f0d2012-05-04 01:10:59 -04004872 if ((*ns == -1) && PyErr_Occurred())
Larry Hastings76ad59b2012-05-03 00:30:07 -07004873 goto exit;
4874
4875 result = 1;
4876exit:
4877 Py_XDECREF(divmod);
4878 return result;
4879}
4880
Larry Hastings2f936352014-08-05 14:04:04 +10004881
4882/*[clinic input]
4883os.utime
4884
4885 path: path_t(allow_fd='PATH_UTIME_HAVE_FD')
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004886 times: object = None
Larry Hastings2f936352014-08-05 14:04:04 +10004887 *
4888 ns: object = NULL
4889 dir_fd: dir_fd(requires='futimensat') = None
4890 follow_symlinks: bool=True
4891
Martin Panter0ff89092015-09-09 01:56:53 +00004892# "utime(path, times=None, *[, ns], dir_fd=None, follow_symlinks=True)\n\
Larry Hastings2f936352014-08-05 14:04:04 +10004893
4894Set the access and modified time of path.
4895
4896path may always be specified as a string.
4897On some platforms, path may also be specified as an open file descriptor.
4898 If this functionality is unavailable, using it raises an exception.
4899
4900If times is not None, it must be a tuple (atime, mtime);
4901 atime and mtime should be expressed as float seconds since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004902If ns is specified, it must be a tuple (atime_ns, mtime_ns);
Larry Hastings2f936352014-08-05 14:04:04 +10004903 atime_ns and mtime_ns should be expressed as integer nanoseconds
4904 since the epoch.
Martin Panter0ff89092015-09-09 01:56:53 +00004905If times is None and ns is unspecified, utime uses the current time.
Larry Hastings2f936352014-08-05 14:04:04 +10004906Specifying tuples for both times and ns is an error.
4907
4908If dir_fd is not None, it should be a file descriptor open to a directory,
4909 and path should be relative; path will then be relative to that directory.
4910If follow_symlinks is False, and the last element of the path is a symbolic
4911 link, utime will modify the symbolic link itself instead of the file the
4912 link points to.
4913It is an error to use dir_fd or follow_symlinks when specifying path
4914 as an open file descriptor.
4915dir_fd and follow_symlinks may not be available on your platform.
4916 If they are unavailable, using them will raise a NotImplementedError.
4917
4918[clinic start generated code]*/
4919
Larry Hastings2f936352014-08-05 14:04:04 +10004920static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03004921os_utime_impl(PyObject *module, path_t *path, PyObject *times, PyObject *ns,
4922 int dir_fd, int follow_symlinks)
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004923/*[clinic end generated code: output=cfcac69d027b82cf input=2fbd62a2f228f8f4]*/
Larry Hastings2f936352014-08-05 14:04:04 +10004924{
Larry Hastings9cf065c2012-06-22 16:30:09 -07004925#ifdef MS_WINDOWS
4926 HANDLE hFile;
4927 FILETIME atime, mtime;
4928#else
4929 int result;
4930#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07004931
Larry Hastings2f936352014-08-05 14:04:04 +10004932 utime_t utime;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004933
Christian Heimesb3c87242013-08-01 00:08:16 +02004934 memset(&utime, 0, sizeof(utime_t));
Larry Hastings76ad59b2012-05-03 00:30:07 -07004935
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004936 if (times != Py_None && ns) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004937 PyErr_SetString(PyExc_ValueError,
4938 "utime: you may specify either 'times'"
4939 " or 'ns' but not both");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004940 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004941 }
4942
Serhiy Storchaka279f4462019-09-14 12:24:05 +03004943 if (times != Py_None) {
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004944 time_t a_sec, m_sec;
4945 long a_nsec, m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004946 if (!PyTuple_CheckExact(times) || (PyTuple_Size(times) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004947 PyErr_SetString(PyExc_TypeError,
4948 "utime: 'times' must be either"
4949 " a tuple of two ints or None");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004950 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004951 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004952 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004953 if (_PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 0),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004954 &a_sec, &a_nsec, _PyTime_ROUND_FLOOR) == -1 ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004955 _PyTime_ObjectToTimespec(PyTuple_GET_ITEM(times, 1),
Victor Stinnerdca028b2015-03-30 01:02:57 +02004956 &m_sec, &m_nsec, _PyTime_ROUND_FLOOR) == -1) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004957 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004958 }
Antoine Pitroucf8a1e52013-04-17 22:06:44 +02004959 utime.atime_s = a_sec;
4960 utime.atime_ns = a_nsec;
4961 utime.mtime_s = m_sec;
4962 utime.mtime_ns = m_nsec;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004963 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004964 else if (ns) {
Larry Hastings76ad59b2012-05-03 00:30:07 -07004965 if (!PyTuple_CheckExact(ns) || (PyTuple_Size(ns) != 2)) {
Larry Hastings9cf065c2012-06-22 16:30:09 -07004966 PyErr_SetString(PyExc_TypeError,
4967 "utime: 'ns' must be a tuple of two ints");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004968 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004969 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004970 utime.now = 0;
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004971 if (!split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 0),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004972 &utime.atime_s, &utime.atime_ns) ||
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004973 !split_py_long_to_s_and_ns(PyTuple_GET_ITEM(ns, 1),
Larry Hastings9cf065c2012-06-22 16:30:09 -07004974 &utime.mtime_s, &utime.mtime_ns)) {
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004975 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07004976 }
Larry Hastings9cf065c2012-06-22 16:30:09 -07004977 }
4978 else {
4979 /* times and ns are both None/unspecified. use "now". */
4980 utime.now = 1;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004981 }
4982
Victor Stinner4552ced2015-09-21 22:37:15 +02004983#if !defined(UTIME_HAVE_NOFOLLOW_SYMLINKS)
Larry Hastings9cf065c2012-06-22 16:30:09 -07004984 if (follow_symlinks_specified("utime", follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004985 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004986#endif
Benjamin Petersonb399ab22012-05-04 01:31:13 -04004987
Larry Hastings2f936352014-08-05 14:04:04 +10004988 if (path_and_dir_fd_invalid("utime", path, dir_fd) ||
4989 dir_fd_and_fd_invalid("utime", dir_fd, path->fd) ||
4990 fd_and_follow_symlinks_invalid("utime", path->fd, follow_symlinks))
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004991 return NULL;
Larry Hastings76ad59b2012-05-03 00:30:07 -07004992
Larry Hastings9cf065c2012-06-22 16:30:09 -07004993#if !defined(HAVE_UTIMENSAT)
4994 if ((dir_fd != DEFAULT_DIR_FD) && (!follow_symlinks)) {
Georg Brandl969288e2012-06-26 09:25:44 +02004995 PyErr_SetString(PyExc_ValueError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07004996 "utime: cannot use dir_fd and follow_symlinks "
4997 "together on this platform");
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02004998 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -07004999 }
5000#endif
Larry Hastings76ad59b2012-05-03 00:30:07 -07005001
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005002 if (PySys_Audit("os.utime", "OOOi", path->object, times, ns ? ns : Py_None,
5003 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
5004 return NULL;
5005 }
5006
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005007#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005008 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -07005009 hFile = CreateFileW(path->wide, FILE_WRITE_ATTRIBUTES, 0,
5010 NULL, OPEN_EXISTING,
5011 FILE_FLAG_BACKUP_SEMANTICS, NULL);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005012 Py_END_ALLOW_THREADS
5013 if (hFile == INVALID_HANDLE_VALUE) {
Larry Hastings2f936352014-08-05 14:04:04 +10005014 path_error(path);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005015 return NULL;
Larry Hastingsb3336402012-05-04 02:31:57 -07005016 }
5017
Larry Hastings9cf065c2012-06-22 16:30:09 -07005018 if (utime.now) {
Antoine Pitrou91a7af32013-11-23 15:23:26 +01005019 GetSystemTimeAsFileTime(&mtime);
5020 atime = mtime;
Victor Stinner8c62be82010-05-06 00:08:46 +00005021 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005022 else {
Steve Dowerbf1f3762015-02-21 15:26:02 -08005023 _Py_time_t_to_FILE_TIME(utime.atime_s, utime.atime_ns, &atime);
5024 _Py_time_t_to_FILE_TIME(utime.mtime_s, utime.mtime_ns, &mtime);
Victor Stinner8c62be82010-05-06 00:08:46 +00005025 }
5026 if (!SetFileTime(hFile, NULL, &atime, &mtime)) {
5027 /* Avoid putting the file name into the error here,
5028 as that may confuse the user into believing that
5029 something is wrong with the file, when it also
5030 could be the time stamp that gives a problem. */
Victor Stinnerb024e842012-10-31 22:24:06 +01005031 PyErr_SetFromWindowsErr(0);
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005032 CloseHandle(hFile);
5033 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005034 }
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005035 CloseHandle(hFile);
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005036#else /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005037 Py_BEGIN_ALLOW_THREADS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00005038
Victor Stinner4552ced2015-09-21 22:37:15 +02005039#ifdef UTIME_HAVE_NOFOLLOW_SYMLINKS
Larry Hastings9cf065c2012-06-22 16:30:09 -07005040 if ((!follow_symlinks) && (dir_fd == DEFAULT_DIR_FD))
Larry Hastings2f936352014-08-05 14:04:04 +10005041 result = utime_nofollow_symlinks(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005042 else
Larry Hastings9e3e70b2011-09-08 19:29:07 -07005043#endif
Larry Hastings9cf065c2012-06-22 16:30:09 -07005044
Victor Stinner528a9ab2015-09-03 21:30:26 +02005045#if defined(HAVE_FUTIMESAT) || defined(HAVE_UTIMENSAT)
Larry Hastings9cf065c2012-06-22 16:30:09 -07005046 if ((dir_fd != DEFAULT_DIR_FD) || (!follow_symlinks))
Larry Hastings2f936352014-08-05 14:04:04 +10005047 result = utime_dir_fd(&utime, dir_fd, path->narrow, follow_symlinks);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005048 else
5049#endif
5050
Victor Stinner528a9ab2015-09-03 21:30:26 +02005051#if defined(HAVE_FUTIMES) || defined(HAVE_FUTIMENS)
Larry Hastings2f936352014-08-05 14:04:04 +10005052 if (path->fd != -1)
5053 result = utime_fd(&utime, path->fd);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005054 else
5055#endif
5056
Larry Hastings2f936352014-08-05 14:04:04 +10005057 result = utime_default(&utime, path->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005058
5059 Py_END_ALLOW_THREADS
5060
5061 if (result < 0) {
5062 /* see previous comment about not putting filename in error here */
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005063 posix_error();
5064 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00005065 }
Larry Hastings76ad59b2012-05-03 00:30:07 -07005066
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +00005067#endif /* MS_WINDOWS */
Larry Hastings9cf065c2012-06-22 16:30:09 -07005068
Serhiy Storchaka32bc11c2018-12-01 14:30:20 +02005069 Py_RETURN_NONE;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005070}
5071
Guido van Rossum3b066191991-06-04 19:40:25 +00005072/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005073
Larry Hastings2f936352014-08-05 14:04:04 +10005074
5075/*[clinic input]
5076os._exit
5077
5078 status: int
5079
5080Exit to the system with specified status, without normal exit processing.
5081[clinic start generated code]*/
5082
Larry Hastings2f936352014-08-05 14:04:04 +10005083static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005084os__exit_impl(PyObject *module, int status)
5085/*[clinic end generated code: output=116e52d9c2260d54 input=5e6d57556b0c4a62]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005086{
5087 _exit(status);
Victor Stinner8c62be82010-05-06 00:08:46 +00005088 return NULL; /* Make gcc -Wall happy */
Guido van Rossum85e3b011991-06-03 12:42:10 +00005089}
5090
Steve Dowercc16be82016-09-08 10:35:16 -07005091#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5092#define EXECV_CHAR wchar_t
5093#else
5094#define EXECV_CHAR char
5095#endif
5096
pxinwrf2d7ac72019-05-21 18:46:37 +08005097#if defined(HAVE_EXECV) || defined(HAVE_SPAWNV) || defined(HAVE_RTPSPAWN)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005098static void
Steve Dowercc16be82016-09-08 10:35:16 -07005099free_string_array(EXECV_CHAR **array, Py_ssize_t count)
Martin v. Löwis114619e2002-10-07 06:44:21 +00005100{
Victor Stinner8c62be82010-05-06 00:08:46 +00005101 Py_ssize_t i;
5102 for (i = 0; i < count; i++)
5103 PyMem_Free(array[i]);
5104 PyMem_DEL(array);
Martin v. Löwis114619e2002-10-07 06:44:21 +00005105}
Martin v. Löwis011e8422009-05-05 04:43:17 +00005106
Berker Peksag81816462016-09-15 20:19:47 +03005107static int
5108fsconvert_strdup(PyObject *o, EXECV_CHAR **out)
Martin v. Löwis011e8422009-05-05 04:43:17 +00005109{
Victor Stinner8c62be82010-05-06 00:08:46 +00005110 Py_ssize_t size;
Berker Peksag81816462016-09-15 20:19:47 +03005111 PyObject *ub;
5112 int result = 0;
Steve Dowercc16be82016-09-08 10:35:16 -07005113#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
Berker Peksag81816462016-09-15 20:19:47 +03005114 if (!PyUnicode_FSDecoder(o, &ub))
Steve Dowercc16be82016-09-08 10:35:16 -07005115 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005116 *out = PyUnicode_AsWideCharString(ub, &size);
5117 if (*out)
5118 result = 1;
Steve Dowercc16be82016-09-08 10:35:16 -07005119#else
Berker Peksag81816462016-09-15 20:19:47 +03005120 if (!PyUnicode_FSConverter(o, &ub))
Victor Stinner8c62be82010-05-06 00:08:46 +00005121 return 0;
Berker Peksag81816462016-09-15 20:19:47 +03005122 size = PyBytes_GET_SIZE(ub);
5123 *out = PyMem_Malloc(size + 1);
5124 if (*out) {
5125 memcpy(*out, PyBytes_AS_STRING(ub), size + 1);
5126 result = 1;
5127 } else
Victor Stinner50abf222013-11-07 23:56:10 +01005128 PyErr_NoMemory();
Steve Dowercc16be82016-09-08 10:35:16 -07005129#endif
Berker Peksag81816462016-09-15 20:19:47 +03005130 Py_DECREF(ub);
5131 return result;
Martin v. Löwis011e8422009-05-05 04:43:17 +00005132}
Martin v. Löwis114619e2002-10-07 06:44:21 +00005133#endif
5134
pxinwrf2d7ac72019-05-21 18:46:37 +08005135#if defined(HAVE_EXECV) || defined (HAVE_FEXECVE) || defined(HAVE_RTPSPAWN)
Steve Dowercc16be82016-09-08 10:35:16 -07005136static EXECV_CHAR**
Victor Stinner13bb71c2010-04-23 21:41:56 +00005137parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
5138{
Victor Stinner8c62be82010-05-06 00:08:46 +00005139 Py_ssize_t i, pos, envc;
5140 PyObject *keys=NULL, *vals=NULL;
Berker Peksag81816462016-09-15 20:19:47 +03005141 PyObject *key, *val, *key2, *val2, *keyval;
Steve Dowercc16be82016-09-08 10:35:16 -07005142 EXECV_CHAR **envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005143
Victor Stinner8c62be82010-05-06 00:08:46 +00005144 i = PyMapping_Size(env);
5145 if (i < 0)
5146 return NULL;
Steve Dowercc16be82016-09-08 10:35:16 -07005147 envlist = PyMem_NEW(EXECV_CHAR *, i + 1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005148 if (envlist == NULL) {
5149 PyErr_NoMemory();
5150 return NULL;
5151 }
5152 envc = 0;
5153 keys = PyMapping_Keys(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005154 if (!keys)
5155 goto error;
Victor Stinner8c62be82010-05-06 00:08:46 +00005156 vals = PyMapping_Values(env);
Victor Stinnerb0314272013-11-14 21:37:05 +01005157 if (!vals)
Victor Stinner8c62be82010-05-06 00:08:46 +00005158 goto error;
5159 if (!PyList_Check(keys) || !PyList_Check(vals)) {
5160 PyErr_Format(PyExc_TypeError,
5161 "env.keys() or env.values() is not a list");
5162 goto error;
5163 }
Victor Stinner13bb71c2010-04-23 21:41:56 +00005164
Victor Stinner8c62be82010-05-06 00:08:46 +00005165 for (pos = 0; pos < i; pos++) {
5166 key = PyList_GetItem(keys, pos);
5167 val = PyList_GetItem(vals, pos);
5168 if (!key || !val)
5169 goto error;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005170
Berker Peksag81816462016-09-15 20:19:47 +03005171#if defined(HAVE_WEXECV) || defined(HAVE_WSPAWNV)
5172 if (!PyUnicode_FSDecoder(key, &key2))
5173 goto error;
5174 if (!PyUnicode_FSDecoder(val, &val2)) {
5175 Py_DECREF(key2);
5176 goto error;
5177 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005178 /* Search from index 1 because on Windows starting '=' is allowed for
5179 defining hidden environment variables. */
5180 if (PyUnicode_GET_LENGTH(key2) == 0 ||
5181 PyUnicode_FindChar(key2, '=', 1, PyUnicode_GET_LENGTH(key2), 1) != -1)
5182 {
5183 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005184 Py_DECREF(key2);
5185 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005186 goto error;
5187 }
Berker Peksag81816462016-09-15 20:19:47 +03005188 keyval = PyUnicode_FromFormat("%U=%U", key2, val2);
5189#else
5190 if (!PyUnicode_FSConverter(key, &key2))
5191 goto error;
5192 if (!PyUnicode_FSConverter(val, &val2)) {
5193 Py_DECREF(key2);
5194 goto error;
5195 }
Serhiy Storchaka77703942017-06-25 07:33:01 +03005196 if (PyBytes_GET_SIZE(key2) == 0 ||
5197 strchr(PyBytes_AS_STRING(key2) + 1, '=') != NULL)
5198 {
5199 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
Eric N. Vander Weelea7874c72017-06-26 21:35:20 -04005200 Py_DECREF(key2);
5201 Py_DECREF(val2);
Serhiy Storchaka77703942017-06-25 07:33:01 +03005202 goto error;
5203 }
Berker Peksag81816462016-09-15 20:19:47 +03005204 keyval = PyBytes_FromFormat("%s=%s", PyBytes_AS_STRING(key2),
5205 PyBytes_AS_STRING(val2));
5206#endif
5207 Py_DECREF(key2);
5208 Py_DECREF(val2);
Steve Dowercc16be82016-09-08 10:35:16 -07005209 if (!keyval)
Victor Stinner8c62be82010-05-06 00:08:46 +00005210 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -07005211
5212 if (!fsconvert_strdup(keyval, &envlist[envc++])) {
5213 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005214 goto error;
5215 }
Berker Peksag81816462016-09-15 20:19:47 +03005216
Steve Dowercc16be82016-09-08 10:35:16 -07005217 Py_DECREF(keyval);
Victor Stinner8c62be82010-05-06 00:08:46 +00005218 }
5219 Py_DECREF(vals);
5220 Py_DECREF(keys);
Victor Stinner13bb71c2010-04-23 21:41:56 +00005221
Victor Stinner8c62be82010-05-06 00:08:46 +00005222 envlist[envc] = 0;
5223 *envc_ptr = envc;
5224 return envlist;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005225
5226error:
Victor Stinner8c62be82010-05-06 00:08:46 +00005227 Py_XDECREF(keys);
5228 Py_XDECREF(vals);
Steve Dowercc16be82016-09-08 10:35:16 -07005229 free_string_array(envlist, envc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005230 return NULL;
Victor Stinner13bb71c2010-04-23 21:41:56 +00005231}
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005232
Steve Dowercc16be82016-09-08 10:35:16 -07005233static EXECV_CHAR**
Ross Lagerwall7807c352011-03-17 20:20:30 +02005234parse_arglist(PyObject* argv, Py_ssize_t *argc)
5235{
5236 int i;
Steve Dowercc16be82016-09-08 10:35:16 -07005237 EXECV_CHAR **argvlist = PyMem_NEW(EXECV_CHAR *, *argc+1);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005238 if (argvlist == NULL) {
5239 PyErr_NoMemory();
5240 return NULL;
5241 }
5242 for (i = 0; i < *argc; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005243 PyObject* item = PySequence_ITEM(argv, i);
5244 if (item == NULL)
5245 goto fail;
5246 if (!fsconvert_strdup(item, &argvlist[i])) {
5247 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005248 goto fail;
5249 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005250 Py_DECREF(item);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005251 }
5252 argvlist[*argc] = NULL;
5253 return argvlist;
5254fail:
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02005255 *argc = i;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005256 free_string_array(argvlist, *argc);
5257 return NULL;
5258}
Steve Dowercc16be82016-09-08 10:35:16 -07005259
Ross Lagerwall7807c352011-03-17 20:20:30 +02005260#endif
5261
Larry Hastings2f936352014-08-05 14:04:04 +10005262
Ross Lagerwall7807c352011-03-17 20:20:30 +02005263#ifdef HAVE_EXECV
Larry Hastings2f936352014-08-05 14:04:04 +10005264/*[clinic input]
5265os.execv
5266
Steve Dowercc16be82016-09-08 10:35:16 -07005267 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005268 Path of executable file.
5269 argv: object
5270 Tuple or list of strings.
5271 /
5272
5273Execute an executable path with arguments, replacing current process.
5274[clinic start generated code]*/
5275
Larry Hastings2f936352014-08-05 14:04:04 +10005276static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005277os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
5278/*[clinic end generated code: output=3b52fec34cd0dafd input=9bac31efae07dac7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005279{
Steve Dowercc16be82016-09-08 10:35:16 -07005280 EXECV_CHAR **argvlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005281 Py_ssize_t argc;
5282
5283 /* execv has two arguments: (path, argv), where
5284 argv is a list or tuple of strings. */
5285
Ross Lagerwall7807c352011-03-17 20:20:30 +02005286 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
5287 PyErr_SetString(PyExc_TypeError,
5288 "execv() arg 2 must be a tuple or list");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005289 return NULL;
5290 }
5291 argc = PySequence_Size(argv);
5292 if (argc < 1) {
5293 PyErr_SetString(PyExc_ValueError, "execv() arg 2 must not be empty");
Ross Lagerwall7807c352011-03-17 20:20:30 +02005294 return NULL;
5295 }
5296
5297 argvlist = parse_arglist(argv, &argc);
5298 if (argvlist == NULL) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02005299 return NULL;
5300 }
Steve Dowerbce26262016-11-19 19:17:26 -08005301 if (!argvlist[0][0]) {
5302 PyErr_SetString(PyExc_ValueError,
5303 "execv() arg 2 first element cannot be empty");
5304 free_string_array(argvlist, argc);
5305 return NULL;
5306 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005307
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005308 if (PySys_Audit("os.exec", "OOO", path->object, argv, Py_None) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005309 free_string_array(argvlist, argc);
5310 return NULL;
5311 }
5312
Steve Dowerbce26262016-11-19 19:17:26 -08005313 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005314#ifdef HAVE_WEXECV
5315 _wexecv(path->wide, argvlist);
5316#else
5317 execv(path->narrow, argvlist);
5318#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005319 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005320
5321 /* If we get here it's definitely an error */
5322
5323 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005324 return posix_error();
5325}
5326
Larry Hastings2f936352014-08-05 14:04:04 +10005327
5328/*[clinic input]
5329os.execve
5330
5331 path: path_t(allow_fd='PATH_HAVE_FEXECVE')
5332 Path of executable file.
5333 argv: object
5334 Tuple or list of strings.
5335 env: object
5336 Dictionary of strings mapping to strings.
5337
5338Execute an executable path with arguments, replacing current process.
5339[clinic start generated code]*/
5340
Larry Hastings2f936352014-08-05 14:04:04 +10005341static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03005342os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
5343/*[clinic end generated code: output=ff9fa8e4da8bde58 input=626804fa092606d9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005344{
Steve Dowercc16be82016-09-08 10:35:16 -07005345 EXECV_CHAR **argvlist = NULL;
5346 EXECV_CHAR **envlist;
Ross Lagerwall7807c352011-03-17 20:20:30 +02005347 Py_ssize_t argc, envc;
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005348
Victor Stinner8c62be82010-05-06 00:08:46 +00005349 /* execve has three arguments: (path, argv, env), where
5350 argv is a list or tuple of strings and env is a dictionary
5351 like posix.environ. */
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005352
Ross Lagerwall7807c352011-03-17 20:20:30 +02005353 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005354 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005355 "execve: argv must be a tuple or list");
Saiyang Gou95f60012020-02-04 16:15:00 -08005356 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005357 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005358 argc = PySequence_Size(argv);
Steve Dowerbce26262016-11-19 19:17:26 -08005359 if (argc < 1) {
5360 PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
5361 return NULL;
5362 }
5363
Victor Stinner8c62be82010-05-06 00:08:46 +00005364 if (!PyMapping_Check(env)) {
5365 PyErr_SetString(PyExc_TypeError,
Larry Hastings9cf065c2012-06-22 16:30:09 -07005366 "execve: environment must be a mapping object");
Saiyang Gou95f60012020-02-04 16:15:00 -08005367 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005368 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005369
Ross Lagerwall7807c352011-03-17 20:20:30 +02005370 argvlist = parse_arglist(argv, &argc);
Victor Stinner8c62be82010-05-06 00:08:46 +00005371 if (argvlist == NULL) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005372 goto fail_0;
Victor Stinner8c62be82010-05-06 00:08:46 +00005373 }
Steve Dowerbce26262016-11-19 19:17:26 -08005374 if (!argvlist[0][0]) {
5375 PyErr_SetString(PyExc_ValueError,
5376 "execve: argv first element cannot be empty");
Saiyang Gou95f60012020-02-04 16:15:00 -08005377 goto fail_0;
Steve Dowerbce26262016-11-19 19:17:26 -08005378 }
Guido van Rossumc6dcc9f1993-11-05 10:15:19 +00005379
Victor Stinner8c62be82010-05-06 00:08:46 +00005380 envlist = parse_envlist(env, &envc);
5381 if (envlist == NULL)
Saiyang Gou95f60012020-02-04 16:15:00 -08005382 goto fail_0;
5383
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005384 if (PySys_Audit("os.exec", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005385 goto fail_1;
5386 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02005387
Steve Dowerbce26262016-11-19 19:17:26 -08005388 _Py_BEGIN_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07005389#ifdef HAVE_FEXECVE
Larry Hastings2f936352014-08-05 14:04:04 +10005390 if (path->fd > -1)
5391 fexecve(path->fd, argvlist, envlist);
Larry Hastings9cf065c2012-06-22 16:30:09 -07005392 else
5393#endif
Steve Dowercc16be82016-09-08 10:35:16 -07005394#ifdef HAVE_WEXECV
5395 _wexecve(path->wide, argvlist, envlist);
5396#else
Larry Hastings2f936352014-08-05 14:04:04 +10005397 execve(path->narrow, argvlist, envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07005398#endif
Steve Dowerbce26262016-11-19 19:17:26 -08005399 _Py_END_SUPPRESS_IPH
Ross Lagerwall7807c352011-03-17 20:20:30 +02005400
5401 /* If we get here it's definitely an error */
5402
Alexey Izbyshev83460312018-10-20 03:28:22 +03005403 posix_path_error(path);
Saiyang Gou95f60012020-02-04 16:15:00 -08005404 fail_1:
Steve Dowercc16be82016-09-08 10:35:16 -07005405 free_string_array(envlist, envc);
Saiyang Gou95f60012020-02-04 16:15:00 -08005406 fail_0:
Larry Hastings9cf065c2012-06-22 16:30:09 -07005407 if (argvlist)
5408 free_string_array(argvlist, argc);
Ross Lagerwall7807c352011-03-17 20:20:30 +02005409 return NULL;
5410}
Steve Dowercc16be82016-09-08 10:35:16 -07005411
Larry Hastings9cf065c2012-06-22 16:30:09 -07005412#endif /* HAVE_EXECV */
5413
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005414#ifdef HAVE_POSIX_SPAWN
5415
5416enum posix_spawn_file_actions_identifier {
5417 POSIX_SPAWN_OPEN,
5418 POSIX_SPAWN_CLOSE,
5419 POSIX_SPAWN_DUP2
5420};
5421
William Orr81574b82018-10-01 22:19:56 -07005422#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005423static int
Pablo Galindo254a4662018-09-07 16:44:24 +01005424convert_sched_param(PyObject *param, struct sched_param *res);
William Orr81574b82018-10-01 22:19:56 -07005425#endif
Pablo Galindo254a4662018-09-07 16:44:24 +01005426
5427static int
Victor Stinner325e4ba2019-02-01 15:47:24 +01005428parse_posix_spawn_flags(const char *func_name, PyObject *setpgroup,
5429 int resetids, int setsid, PyObject *setsigmask,
Pablo Galindo254a4662018-09-07 16:44:24 +01005430 PyObject *setsigdef, PyObject *scheduler,
5431 posix_spawnattr_t *attrp)
5432{
5433 long all_flags = 0;
5434
5435 errno = posix_spawnattr_init(attrp);
5436 if (errno) {
5437 posix_error();
5438 return -1;
5439 }
5440
5441 if (setpgroup) {
5442 pid_t pgid = PyLong_AsPid(setpgroup);
5443 if (pgid == (pid_t)-1 && PyErr_Occurred()) {
5444 goto fail;
5445 }
5446 errno = posix_spawnattr_setpgroup(attrp, pgid);
5447 if (errno) {
5448 posix_error();
5449 goto fail;
5450 }
5451 all_flags |= POSIX_SPAWN_SETPGROUP;
5452 }
5453
5454 if (resetids) {
5455 all_flags |= POSIX_SPAWN_RESETIDS;
5456 }
5457
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005458 if (setsid) {
5459#ifdef POSIX_SPAWN_SETSID
5460 all_flags |= POSIX_SPAWN_SETSID;
5461#elif defined(POSIX_SPAWN_SETSID_NP)
5462 all_flags |= POSIX_SPAWN_SETSID_NP;
5463#else
5464 argument_unavailable_error(func_name, "setsid");
5465 return -1;
5466#endif
5467 }
5468
Pablo Galindo254a4662018-09-07 16:44:24 +01005469 if (setsigmask) {
5470 sigset_t set;
5471 if (!_Py_Sigset_Converter(setsigmask, &set)) {
5472 goto fail;
5473 }
5474 errno = posix_spawnattr_setsigmask(attrp, &set);
5475 if (errno) {
5476 posix_error();
5477 goto fail;
5478 }
5479 all_flags |= POSIX_SPAWN_SETSIGMASK;
5480 }
5481
5482 if (setsigdef) {
5483 sigset_t set;
5484 if (!_Py_Sigset_Converter(setsigdef, &set)) {
5485 goto fail;
5486 }
5487 errno = posix_spawnattr_setsigdefault(attrp, &set);
5488 if (errno) {
5489 posix_error();
5490 goto fail;
5491 }
5492 all_flags |= POSIX_SPAWN_SETSIGDEF;
5493 }
5494
5495 if (scheduler) {
5496#ifdef POSIX_SPAWN_SETSCHEDULER
5497 PyObject *py_schedpolicy;
5498 struct sched_param schedparam;
5499
5500 if (!PyArg_ParseTuple(scheduler, "OO&"
5501 ";A scheduler tuple must have two elements",
5502 &py_schedpolicy, convert_sched_param, &schedparam)) {
5503 goto fail;
5504 }
5505 if (py_schedpolicy != Py_None) {
5506 int schedpolicy = _PyLong_AsInt(py_schedpolicy);
5507
5508 if (schedpolicy == -1 && PyErr_Occurred()) {
5509 goto fail;
5510 }
5511 errno = posix_spawnattr_setschedpolicy(attrp, schedpolicy);
5512 if (errno) {
5513 posix_error();
5514 goto fail;
5515 }
5516 all_flags |= POSIX_SPAWN_SETSCHEDULER;
5517 }
5518 errno = posix_spawnattr_setschedparam(attrp, &schedparam);
5519 if (errno) {
5520 posix_error();
5521 goto fail;
5522 }
5523 all_flags |= POSIX_SPAWN_SETSCHEDPARAM;
5524#else
5525 PyErr_SetString(PyExc_NotImplementedError,
5526 "The scheduler option is not supported in this system.");
5527 goto fail;
5528#endif
5529 }
5530
5531 errno = posix_spawnattr_setflags(attrp, all_flags);
5532 if (errno) {
5533 posix_error();
5534 goto fail;
5535 }
5536
5537 return 0;
5538
5539fail:
5540 (void)posix_spawnattr_destroy(attrp);
5541 return -1;
5542}
5543
5544static int
Serhiy Storchakaef347532018-05-01 16:45:04 +03005545parse_file_actions(PyObject *file_actions,
Pablo Galindocb970732018-06-19 09:19:50 +01005546 posix_spawn_file_actions_t *file_actionsp,
5547 PyObject *temp_buffer)
Serhiy Storchakaef347532018-05-01 16:45:04 +03005548{
5549 PyObject *seq;
5550 PyObject *file_action = NULL;
5551 PyObject *tag_obj;
5552
5553 seq = PySequence_Fast(file_actions,
5554 "file_actions must be a sequence or None");
5555 if (seq == NULL) {
5556 return -1;
5557 }
5558
5559 errno = posix_spawn_file_actions_init(file_actionsp);
5560 if (errno) {
5561 posix_error();
5562 Py_DECREF(seq);
5563 return -1;
5564 }
5565
Zackery Spytzd52a83a2019-06-26 14:54:20 -06005566 for (Py_ssize_t i = 0; i < PySequence_Fast_GET_SIZE(seq); ++i) {
Serhiy Storchakaef347532018-05-01 16:45:04 +03005567 file_action = PySequence_Fast_GET_ITEM(seq, i);
5568 Py_INCREF(file_action);
5569 if (!PyTuple_Check(file_action) || !PyTuple_GET_SIZE(file_action)) {
5570 PyErr_SetString(PyExc_TypeError,
5571 "Each file_actions element must be a non-empty tuple");
5572 goto fail;
5573 }
5574 long tag = PyLong_AsLong(PyTuple_GET_ITEM(file_action, 0));
5575 if (tag == -1 && PyErr_Occurred()) {
5576 goto fail;
5577 }
5578
5579 /* Populate the file_actions object */
5580 switch (tag) {
5581 case POSIX_SPAWN_OPEN: {
5582 int fd, oflag;
5583 PyObject *path;
5584 unsigned long mode;
5585 if (!PyArg_ParseTuple(file_action, "OiO&ik"
5586 ";A open file_action tuple must have 5 elements",
5587 &tag_obj, &fd, PyUnicode_FSConverter, &path,
5588 &oflag, &mode))
5589 {
5590 goto fail;
5591 }
Pablo Galindocb970732018-06-19 09:19:50 +01005592 if (PyList_Append(temp_buffer, path)) {
5593 Py_DECREF(path);
5594 goto fail;
5595 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005596 errno = posix_spawn_file_actions_addopen(file_actionsp,
5597 fd, PyBytes_AS_STRING(path), oflag, (mode_t)mode);
Pablo Galindocb970732018-06-19 09:19:50 +01005598 Py_DECREF(path);
Serhiy Storchakaef347532018-05-01 16:45:04 +03005599 if (errno) {
5600 posix_error();
5601 goto fail;
5602 }
5603 break;
5604 }
5605 case POSIX_SPAWN_CLOSE: {
5606 int fd;
5607 if (!PyArg_ParseTuple(file_action, "Oi"
5608 ";A close file_action tuple must have 2 elements",
5609 &tag_obj, &fd))
5610 {
5611 goto fail;
5612 }
5613 errno = posix_spawn_file_actions_addclose(file_actionsp, fd);
5614 if (errno) {
5615 posix_error();
5616 goto fail;
5617 }
5618 break;
5619 }
5620 case POSIX_SPAWN_DUP2: {
5621 int fd1, fd2;
5622 if (!PyArg_ParseTuple(file_action, "Oii"
5623 ";A dup2 file_action tuple must have 3 elements",
5624 &tag_obj, &fd1, &fd2))
5625 {
5626 goto fail;
5627 }
5628 errno = posix_spawn_file_actions_adddup2(file_actionsp,
5629 fd1, fd2);
5630 if (errno) {
5631 posix_error();
5632 goto fail;
5633 }
5634 break;
5635 }
5636 default: {
5637 PyErr_SetString(PyExc_TypeError,
5638 "Unknown file_actions identifier");
5639 goto fail;
5640 }
5641 }
5642 Py_DECREF(file_action);
5643 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005644
Serhiy Storchakaef347532018-05-01 16:45:04 +03005645 Py_DECREF(seq);
5646 return 0;
5647
5648fail:
5649 Py_DECREF(seq);
5650 Py_DECREF(file_action);
5651 (void)posix_spawn_file_actions_destroy(file_actionsp);
5652 return -1;
5653}
5654
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005655
5656static PyObject *
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005657py_posix_spawn(int use_posix_spawnp, PyObject *module, path_t *path, PyObject *argv,
5658 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005659 PyObject *setpgroup, int resetids, int setsid, PyObject *setsigmask,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005660 PyObject *setsigdef, PyObject *scheduler)
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005661{
Victor Stinner325e4ba2019-02-01 15:47:24 +01005662 const char *func_name = use_posix_spawnp ? "posix_spawnp" : "posix_spawn";
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005663 EXECV_CHAR **argvlist = NULL;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005664 EXECV_CHAR **envlist = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005665 posix_spawn_file_actions_t file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005666 posix_spawn_file_actions_t *file_actionsp = NULL;
Pablo Galindo254a4662018-09-07 16:44:24 +01005667 posix_spawnattr_t attr;
5668 posix_spawnattr_t *attrp = NULL;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005669 Py_ssize_t argc, envc;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005670 PyObject *result = NULL;
Pablo Galindocb970732018-06-19 09:19:50 +01005671 PyObject *temp_buffer = NULL;
Serhiy Storchakaef347532018-05-01 16:45:04 +03005672 pid_t pid;
5673 int err_code;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005674
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005675 /* posix_spawn and posix_spawnp have three arguments: (path, argv, env), where
Serhiy Storchakaef347532018-05-01 16:45:04 +03005676 argv is a list or tuple of strings and env is a dictionary
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005677 like posix.environ. */
5678
Serhiy Storchakaef347532018-05-01 16:45:04 +03005679 if (!PyList_Check(argv) && !PyTuple_Check(argv)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005680 PyErr_Format(PyExc_TypeError,
5681 "%s: argv must be a tuple or list", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005682 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005683 }
5684 argc = PySequence_Size(argv);
5685 if (argc < 1) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005686 PyErr_Format(PyExc_ValueError,
5687 "%s: argv must not be empty", func_name);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005688 return NULL;
5689 }
5690
5691 if (!PyMapping_Check(env)) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005692 PyErr_Format(PyExc_TypeError,
5693 "%s: environment must be a mapping object", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005694 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005695 }
5696
5697 argvlist = parse_arglist(argv, &argc);
5698 if (argvlist == NULL) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005699 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005700 }
5701 if (!argvlist[0][0]) {
Victor Stinner325e4ba2019-02-01 15:47:24 +01005702 PyErr_Format(PyExc_ValueError,
5703 "%s: argv first element cannot be empty", func_name);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005704 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005705 }
5706
5707 envlist = parse_envlist(env, &envc);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005708 if (envlist == NULL) {
5709 goto exit;
5710 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005711
Anthony Shaw948ed8c2019-05-10 12:00:06 +10005712 if (file_actions != NULL && file_actions != Py_None) {
Pablo Galindocb970732018-06-19 09:19:50 +01005713 /* There is a bug in old versions of glibc that makes some of the
5714 * helper functions for manipulating file actions not copy the provided
5715 * buffers. The problem is that posix_spawn_file_actions_addopen does not
5716 * copy the value of path for some old versions of glibc (<2.20).
5717 * The use of temp_buffer here is a workaround that keeps the
5718 * python objects that own the buffers alive until posix_spawn gets called.
5719 * Check https://bugs.python.org/issue33630 and
5720 * https://sourceware.org/bugzilla/show_bug.cgi?id=17048 for more info.*/
5721 temp_buffer = PyList_New(0);
5722 if (!temp_buffer) {
5723 goto exit;
5724 }
5725 if (parse_file_actions(file_actions, &file_actions_buf, temp_buffer)) {
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005726 goto exit;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005727 }
Serhiy Storchakaef347532018-05-01 16:45:04 +03005728 file_actionsp = &file_actions_buf;
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005729 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005730
Victor Stinner325e4ba2019-02-01 15:47:24 +01005731 if (parse_posix_spawn_flags(func_name, setpgroup, resetids, setsid,
5732 setsigmask, setsigdef, scheduler, &attr)) {
Pablo Galindo254a4662018-09-07 16:44:24 +01005733 goto exit;
5734 }
5735 attrp = &attr;
5736
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005737 if (PySys_Audit("os.posix_spawn", "OOO", path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08005738 goto exit;
5739 }
5740
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005741 _Py_BEGIN_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005742#ifdef HAVE_POSIX_SPAWNP
5743 if (use_posix_spawnp) {
5744 err_code = posix_spawnp(&pid, path->narrow,
5745 file_actionsp, attrp, argvlist, envlist);
5746 }
5747 else
5748#endif /* HAVE_POSIX_SPAWNP */
5749 {
5750 err_code = posix_spawn(&pid, path->narrow,
5751 file_actionsp, attrp, argvlist, envlist);
5752 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005753 _Py_END_SUPPRESS_IPH
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005754
Serhiy Storchakaef347532018-05-01 16:45:04 +03005755 if (err_code) {
5756 errno = err_code;
5757 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005758 goto exit;
5759 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08005760#ifdef _Py_MEMORY_SANITIZER
5761 __msan_unpoison(&pid, sizeof(pid));
5762#endif
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005763 result = PyLong_FromPid(pid);
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005764
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005765exit:
Serhiy Storchakaef347532018-05-01 16:45:04 +03005766 if (file_actionsp) {
5767 (void)posix_spawn_file_actions_destroy(file_actionsp);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005768 }
Pablo Galindo254a4662018-09-07 16:44:24 +01005769 if (attrp) {
5770 (void)posix_spawnattr_destroy(attrp);
5771 }
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005772 if (envlist) {
5773 free_string_array(envlist, envc);
5774 }
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005775 if (argvlist) {
5776 free_string_array(argvlist, argc);
5777 }
Pablo Galindocb970732018-06-19 09:19:50 +01005778 Py_XDECREF(temp_buffer);
Pablo Galindo0cd6bca2018-01-29 20:34:42 +00005779 return result;
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005780}
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005781
5782
5783/*[clinic input]
5784
5785os.posix_spawn
5786 path: path_t
5787 Path of executable file.
5788 argv: object
5789 Tuple or list of strings.
5790 env: object
5791 Dictionary of strings mapping to strings.
5792 /
5793 *
5794 file_actions: object(c_default='NULL') = ()
5795 A sequence of file action tuples.
5796 setpgroup: object = NULL
5797 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5798 resetids: bool(accept={int}) = False
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005799 If the value is `true` the POSIX_SPAWN_RESETIDS will be activated.
5800 setsid: bool(accept={int}) = False
5801 If the value is `true` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005802 setsigmask: object(c_default='NULL') = ()
5803 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5804 setsigdef: object(c_default='NULL') = ()
5805 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5806 scheduler: object = NULL
5807 A tuple with the scheduler policy (optional) and parameters.
5808
5809Execute the program specified by path in a new process.
5810[clinic start generated code]*/
5811
5812static PyObject *
5813os_posix_spawn_impl(PyObject *module, path_t *path, PyObject *argv,
5814 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005815 PyObject *setpgroup, int resetids, int setsid,
5816 PyObject *setsigmask, PyObject *setsigdef,
5817 PyObject *scheduler)
5818/*[clinic end generated code: output=14a1098c566bc675 input=8c6305619a00ad04]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005819{
5820 return py_posix_spawn(0, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005821 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005822 scheduler);
5823}
5824 #endif /* HAVE_POSIX_SPAWN */
5825
5826
5827
5828#ifdef HAVE_POSIX_SPAWNP
5829/*[clinic input]
5830
5831os.posix_spawnp
5832 path: path_t
5833 Path of executable file.
5834 argv: object
5835 Tuple or list of strings.
5836 env: object
5837 Dictionary of strings mapping to strings.
5838 /
5839 *
5840 file_actions: object(c_default='NULL') = ()
5841 A sequence of file action tuples.
5842 setpgroup: object = NULL
5843 The pgroup to use with the POSIX_SPAWN_SETPGROUP flag.
5844 resetids: bool(accept={int}) = False
5845 If the value is `True` the POSIX_SPAWN_RESETIDS will be activated.
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005846 setsid: bool(accept={int}) = False
5847 If the value is `True` the POSIX_SPAWN_SETSID or POSIX_SPAWN_SETSID_NP will be activated.
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005848 setsigmask: object(c_default='NULL') = ()
5849 The sigmask to use with the POSIX_SPAWN_SETSIGMASK flag.
5850 setsigdef: object(c_default='NULL') = ()
5851 The sigmask to use with the POSIX_SPAWN_SETSIGDEF flag.
5852 scheduler: object = NULL
5853 A tuple with the scheduler policy (optional) and parameters.
5854
5855Execute the program specified by path in a new process.
5856[clinic start generated code]*/
5857
5858static PyObject *
5859os_posix_spawnp_impl(PyObject *module, path_t *path, PyObject *argv,
5860 PyObject *env, PyObject *file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005861 PyObject *setpgroup, int resetids, int setsid,
5862 PyObject *setsigmask, PyObject *setsigdef,
5863 PyObject *scheduler)
5864/*[clinic end generated code: output=7b9aaefe3031238d input=c1911043a22028da]*/
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005865{
5866 return py_posix_spawn(1, module, path, argv, env, file_actions,
Joannah Nanjekye80c5dfe2019-02-01 13:05:22 +03005867 setpgroup, resetids, setsid, setsigmask, setsigdef,
Joannah Nanjekye92b83222019-01-16 16:29:26 +03005868 scheduler);
5869}
5870#endif /* HAVE_POSIX_SPAWNP */
Pablo Galindo6c6ddf92018-01-29 01:56:10 +00005871
pxinwrf2d7ac72019-05-21 18:46:37 +08005872#ifdef HAVE_RTPSPAWN
5873static intptr_t
5874_rtp_spawn(int mode, const char *rtpFileName, const char *argv[],
5875 const char *envp[])
5876{
5877 RTP_ID rtpid;
5878 int status;
5879 pid_t res;
5880 int async_err = 0;
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00005881
pxinwrf2d7ac72019-05-21 18:46:37 +08005882 /* Set priority=100 and uStackSize=16 MiB (0x1000000) for new processes.
5883 uStackSize=0 cannot be used, the default stack size is too small for
5884 Python. */
5885 if (envp) {
5886 rtpid = rtpSpawn(rtpFileName, argv, envp,
5887 100, 0x1000000, 0, VX_FP_TASK);
5888 }
5889 else {
5890 rtpid = rtpSpawn(rtpFileName, argv, (const char **)environ,
5891 100, 0x1000000, 0, VX_FP_TASK);
5892 }
5893 if ((rtpid != RTP_ID_ERROR) && (mode == _P_WAIT)) {
5894 do {
5895 res = waitpid((pid_t)rtpid, &status, 0);
5896 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
5897
5898 if (res < 0)
5899 return RTP_ID_ERROR;
5900 return ((intptr_t)status);
5901 }
5902 return ((intptr_t)rtpid);
5903}
5904#endif
5905
5906#if defined(HAVE_SPAWNV) || defined(HAVE_WSPAWNV) || defined(HAVE_RTPSPAWN)
Larry Hastings2f936352014-08-05 14:04:04 +10005907/*[clinic input]
5908os.spawnv
5909
5910 mode: int
5911 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07005912 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10005913 Path of executable file.
5914 argv: object
5915 Tuple or list of strings.
5916 /
5917
5918Execute the program specified by path in a new process.
5919[clinic start generated code]*/
5920
Larry Hastings2f936352014-08-05 14:04:04 +10005921static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07005922os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
5923/*[clinic end generated code: output=71cd037a9d96b816 input=43224242303291be]*/
Larry Hastings2f936352014-08-05 14:04:04 +10005924{
Steve Dowercc16be82016-09-08 10:35:16 -07005925 EXECV_CHAR **argvlist;
Larry Hastings2f936352014-08-05 14:04:04 +10005926 int i;
Victor Stinner8c62be82010-05-06 00:08:46 +00005927 Py_ssize_t argc;
Benjamin Petersonca470632016-09-06 13:47:26 -07005928 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00005929 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Guido van Rossuma1065681999-01-25 23:20:23 +00005930
Victor Stinner8c62be82010-05-06 00:08:46 +00005931 /* spawnv has three arguments: (mode, path, argv), where
5932 argv is a list or tuple of strings. */
Guido van Rossuma1065681999-01-25 23:20:23 +00005933
Victor Stinner8c62be82010-05-06 00:08:46 +00005934 if (PyList_Check(argv)) {
5935 argc = PyList_Size(argv);
5936 getitem = PyList_GetItem;
5937 }
5938 else if (PyTuple_Check(argv)) {
5939 argc = PyTuple_Size(argv);
5940 getitem = PyTuple_GetItem;
5941 }
5942 else {
5943 PyErr_SetString(PyExc_TypeError,
5944 "spawnv() arg 2 must be a tuple or list");
Victor Stinner8c62be82010-05-06 00:08:46 +00005945 return NULL;
5946 }
Steve Dower859fd7b2016-11-19 18:53:19 -08005947 if (argc == 0) {
5948 PyErr_SetString(PyExc_ValueError,
5949 "spawnv() arg 2 cannot be empty");
5950 return NULL;
5951 }
Guido van Rossuma1065681999-01-25 23:20:23 +00005952
Steve Dowercc16be82016-09-08 10:35:16 -07005953 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00005954 if (argvlist == NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00005955 return PyErr_NoMemory();
5956 }
5957 for (i = 0; i < argc; i++) {
5958 if (!fsconvert_strdup((*getitem)(argv, i),
5959 &argvlist[i])) {
5960 free_string_array(argvlist, i);
5961 PyErr_SetString(
5962 PyExc_TypeError,
5963 "spawnv() arg 2 must contain only strings");
Victor Stinner8c62be82010-05-06 00:08:46 +00005964 return NULL;
5965 }
Steve Dower93ff8722016-11-19 19:03:54 -08005966 if (i == 0 && !argvlist[0][0]) {
Victor Stinner8acb4cf2017-06-15 15:30:40 +02005967 free_string_array(argvlist, i + 1);
Steve Dower93ff8722016-11-19 19:03:54 -08005968 PyErr_SetString(
5969 PyExc_ValueError,
5970 "spawnv() arg 2 first element cannot be empty");
5971 return NULL;
5972 }
Victor Stinner8c62be82010-05-06 00:08:46 +00005973 }
5974 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00005975
pxinwrf2d7ac72019-05-21 18:46:37 +08005976#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00005977 if (mode == _OLD_P_OVERLAY)
5978 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08005979#endif
Tim Peters5aa91602002-01-30 05:46:57 +00005980
Saiyang Gou7514f4f2020-02-12 23:47:42 -08005981 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv,
Saiyang Gou95f60012020-02-04 16:15:00 -08005982 Py_None) < 0) {
5983 free_string_array(argvlist, argc);
5984 return NULL;
5985 }
5986
Victor Stinner8c62be82010-05-06 00:08:46 +00005987 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07005988 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07005989#ifdef HAVE_WSPAWNV
5990 spawnval = _wspawnv(mode, path->wide, argvlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08005991#elif defined(HAVE_RTPSPAWN)
5992 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist, NULL);
Steve Dowercc16be82016-09-08 10:35:16 -07005993#else
5994 spawnval = _spawnv(mode, path->narrow, argvlist);
5995#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07005996 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00005997 Py_END_ALLOW_THREADS
Tim Peters5aa91602002-01-30 05:46:57 +00005998
Victor Stinner8c62be82010-05-06 00:08:46 +00005999 free_string_array(argvlist, argc);
Guido van Rossuma1065681999-01-25 23:20:23 +00006000
Victor Stinner8c62be82010-05-06 00:08:46 +00006001 if (spawnval == -1)
6002 return posix_error();
6003 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006004 return Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006005}
6006
Larry Hastings2f936352014-08-05 14:04:04 +10006007/*[clinic input]
6008os.spawnve
6009
6010 mode: int
6011 Mode of process creation.
Steve Dowercc16be82016-09-08 10:35:16 -07006012 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +10006013 Path of executable file.
6014 argv: object
6015 Tuple or list of strings.
6016 env: object
6017 Dictionary of strings mapping to strings.
6018 /
6019
6020Execute the program specified by path in a new process.
6021[clinic start generated code]*/
6022
Larry Hastings2f936352014-08-05 14:04:04 +10006023static PyObject *
Steve Dowercc16be82016-09-08 10:35:16 -07006024os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006025 PyObject *env)
Steve Dowercc16be82016-09-08 10:35:16 -07006026/*[clinic end generated code: output=30fe85be56fe37ad input=3e40803ee7c4c586]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006027{
Steve Dowercc16be82016-09-08 10:35:16 -07006028 EXECV_CHAR **argvlist;
6029 EXECV_CHAR **envlist;
Victor Stinner8c62be82010-05-06 00:08:46 +00006030 PyObject *res = NULL;
Antoine Pitrou22e41552010-08-15 18:07:50 +00006031 Py_ssize_t argc, i, envc;
Benjamin Petersonca470632016-09-06 13:47:26 -07006032 intptr_t spawnval;
Victor Stinner8c62be82010-05-06 00:08:46 +00006033 PyObject *(*getitem)(PyObject *, Py_ssize_t);
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006034 Py_ssize_t lastarg = 0;
Guido van Rossuma1065681999-01-25 23:20:23 +00006035
Victor Stinner8c62be82010-05-06 00:08:46 +00006036 /* spawnve has four arguments: (mode, path, argv, env), where
6037 argv is a list or tuple of strings and env is a dictionary
6038 like posix.environ. */
Guido van Rossuma1065681999-01-25 23:20:23 +00006039
Victor Stinner8c62be82010-05-06 00:08:46 +00006040 if (PyList_Check(argv)) {
6041 argc = PyList_Size(argv);
6042 getitem = PyList_GetItem;
6043 }
6044 else if (PyTuple_Check(argv)) {
6045 argc = PyTuple_Size(argv);
6046 getitem = PyTuple_GetItem;
6047 }
6048 else {
6049 PyErr_SetString(PyExc_TypeError,
6050 "spawnve() arg 2 must be a tuple or list");
6051 goto fail_0;
6052 }
Steve Dower859fd7b2016-11-19 18:53:19 -08006053 if (argc == 0) {
6054 PyErr_SetString(PyExc_ValueError,
6055 "spawnve() arg 2 cannot be empty");
6056 goto fail_0;
6057 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006058 if (!PyMapping_Check(env)) {
6059 PyErr_SetString(PyExc_TypeError,
6060 "spawnve() arg 3 must be a mapping object");
6061 goto fail_0;
6062 }
Guido van Rossuma1065681999-01-25 23:20:23 +00006063
Steve Dowercc16be82016-09-08 10:35:16 -07006064 argvlist = PyMem_NEW(EXECV_CHAR *, argc+1);
Victor Stinner8c62be82010-05-06 00:08:46 +00006065 if (argvlist == NULL) {
6066 PyErr_NoMemory();
6067 goto fail_0;
6068 }
6069 for (i = 0; i < argc; i++) {
6070 if (!fsconvert_strdup((*getitem)(argv, i),
6071 &argvlist[i]))
6072 {
6073 lastarg = i;
6074 goto fail_1;
6075 }
Steve Dowerbce26262016-11-19 19:17:26 -08006076 if (i == 0 && !argvlist[0][0]) {
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006077 lastarg = i + 1;
Steve Dowerbce26262016-11-19 19:17:26 -08006078 PyErr_SetString(
6079 PyExc_ValueError,
6080 "spawnv() arg 2 first element cannot be empty");
6081 goto fail_1;
6082 }
Victor Stinner8c62be82010-05-06 00:08:46 +00006083 }
6084 lastarg = argc;
6085 argvlist[argc] = NULL;
Guido van Rossuma1065681999-01-25 23:20:23 +00006086
Victor Stinner8c62be82010-05-06 00:08:46 +00006087 envlist = parse_envlist(env, &envc);
6088 if (envlist == NULL)
6089 goto fail_1;
Guido van Rossuma1065681999-01-25 23:20:23 +00006090
pxinwrf2d7ac72019-05-21 18:46:37 +08006091#if !defined(HAVE_RTPSPAWN)
Victor Stinner8c62be82010-05-06 00:08:46 +00006092 if (mode == _OLD_P_OVERLAY)
6093 mode = _P_OVERLAY;
pxinwrf2d7ac72019-05-21 18:46:37 +08006094#endif
Tim Peters25059d32001-12-07 20:35:43 +00006095
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006096 if (PySys_Audit("os.spawn", "iOOO", mode, path->object, argv, env) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -08006097 goto fail_2;
6098 }
6099
Victor Stinner8c62be82010-05-06 00:08:46 +00006100 Py_BEGIN_ALLOW_THREADS
Steve Dower654a7bd2016-09-11 20:19:32 -07006101 _Py_BEGIN_SUPPRESS_IPH
Steve Dowercc16be82016-09-08 10:35:16 -07006102#ifdef HAVE_WSPAWNV
6103 spawnval = _wspawnve(mode, path->wide, argvlist, envlist);
pxinwrf2d7ac72019-05-21 18:46:37 +08006104#elif defined(HAVE_RTPSPAWN)
6105 spawnval = _rtp_spawn(mode, path->narrow, (const char **)argvlist,
6106 (const char **)envlist);
Steve Dowercc16be82016-09-08 10:35:16 -07006107#else
6108 spawnval = _spawnve(mode, path->narrow, argvlist, envlist);
6109#endif
Steve Dower654a7bd2016-09-11 20:19:32 -07006110 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00006111 Py_END_ALLOW_THREADS
Tim Peters25059d32001-12-07 20:35:43 +00006112
Victor Stinner8c62be82010-05-06 00:08:46 +00006113 if (spawnval == -1)
6114 (void) posix_error();
6115 else
Richard Oudkerkac0ad882013-06-05 23:29:30 +01006116 res = Py_BuildValue(_Py_PARSE_INTPTR, spawnval);
Guido van Rossuma1065681999-01-25 23:20:23 +00006117
Saiyang Gou95f60012020-02-04 16:15:00 -08006118 fail_2:
Victor Stinner8c62be82010-05-06 00:08:46 +00006119 while (--envc >= 0)
6120 PyMem_DEL(envlist[envc]);
6121 PyMem_DEL(envlist);
Guido van Rossum0847c5c2002-12-13 18:36:22 +00006122 fail_1:
Victor Stinnerc8d6ab22017-06-23 15:04:46 +02006123 free_string_array(argvlist, lastarg);
Martin v. Löwis114619e2002-10-07 06:44:21 +00006124 fail_0:
Victor Stinner8c62be82010-05-06 00:08:46 +00006125 return res;
Guido van Rossuma1065681999-01-25 23:20:23 +00006126}
Andrew MacIntyre69e18c92004-04-04 07:11:43 +00006127
Guido van Rossuma1065681999-01-25 23:20:23 +00006128#endif /* HAVE_SPAWNV */
6129
6130
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006131#ifdef HAVE_FORK
Gregory P. Smith163468a2017-05-29 10:03:41 -07006132
6133/* Helper function to validate arguments.
6134 Returns 0 on success. non-zero on failure with a TypeError raised.
6135 If obj is non-NULL it must be callable. */
6136static int
6137check_null_or_callable(PyObject *obj, const char* obj_name)
6138{
6139 if (obj && !PyCallable_Check(obj)) {
6140 PyErr_Format(PyExc_TypeError, "'%s' must be callable, not %s",
Eddie Elizondob3966632019-11-05 07:16:14 -08006141 obj_name, _PyType_Name(Py_TYPE(obj)));
Gregory P. Smith163468a2017-05-29 10:03:41 -07006142 return -1;
6143 }
6144 return 0;
6145}
6146
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006147/*[clinic input]
6148os.register_at_fork
6149
Gregory P. Smith163468a2017-05-29 10:03:41 -07006150 *
6151 before: object=NULL
6152 A callable to be called in the parent before the fork() syscall.
6153 after_in_child: object=NULL
6154 A callable to be called in the child after fork().
6155 after_in_parent: object=NULL
6156 A callable to be called in the parent after fork().
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006157
Gregory P. Smith163468a2017-05-29 10:03:41 -07006158Register callables to be called when forking a new process.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006159
Gregory P. Smith163468a2017-05-29 10:03:41 -07006160'before' callbacks are called in reverse order.
6161'after_in_child' and 'after_in_parent' callbacks are called in order.
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006162
6163[clinic start generated code]*/
6164
6165static PyObject *
Gregory P. Smith163468a2017-05-29 10:03:41 -07006166os_register_at_fork_impl(PyObject *module, PyObject *before,
6167 PyObject *after_in_child, PyObject *after_in_parent)
6168/*[clinic end generated code: output=5398ac75e8e97625 input=cd1187aa85d2312e]*/
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006169{
6170 PyInterpreterState *interp;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006171
Gregory P. Smith163468a2017-05-29 10:03:41 -07006172 if (!before && !after_in_child && !after_in_parent) {
6173 PyErr_SetString(PyExc_TypeError, "At least one argument is required.");
6174 return NULL;
6175 }
6176 if (check_null_or_callable(before, "before") ||
6177 check_null_or_callable(after_in_child, "after_in_child") ||
6178 check_null_or_callable(after_in_parent, "after_in_parent")) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006179 return NULL;
6180 }
Victor Stinner81a7be32020-04-14 15:14:01 +02006181 interp = _PyInterpreterState_GET();
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006182
Gregory P. Smith163468a2017-05-29 10:03:41 -07006183 if (register_at_forker(&interp->before_forkers, before)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006184 return NULL;
6185 }
Gregory P. Smith163468a2017-05-29 10:03:41 -07006186 if (register_at_forker(&interp->after_forkers_child, after_in_child)) {
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006187 return NULL;
Gregory P. Smith163468a2017-05-29 10:03:41 -07006188 }
6189 if (register_at_forker(&interp->after_forkers_parent, after_in_parent)) {
6190 return NULL;
6191 }
6192 Py_RETURN_NONE;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006193}
6194#endif /* HAVE_FORK */
6195
6196
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006197#ifdef HAVE_FORK1
Larry Hastings2f936352014-08-05 14:04:04 +10006198/*[clinic input]
6199os.fork1
6200
6201Fork a child process with a single multiplexed (i.e., not bound) thread.
6202
6203Return 0 to child process and PID of child to parent process.
6204[clinic start generated code]*/
6205
Larry Hastings2f936352014-08-05 14:04:04 +10006206static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006207os_fork1_impl(PyObject *module)
6208/*[clinic end generated code: output=0de8e67ce2a310bc input=12db02167893926e]*/
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006209{
Victor Stinner8c62be82010-05-06 00:08:46 +00006210 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006211
Victor Stinner81a7be32020-04-14 15:14:01 +02006212 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006213 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6214 return NULL;
6215 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006216 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006217 pid = fork1();
6218 if (pid == 0) {
6219 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006220 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006221 } else {
6222 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006223 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006224 }
6225 if (pid == -1)
6226 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006227 return PyLong_FromPid(pid);
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006228}
Larry Hastings2f936352014-08-05 14:04:04 +10006229#endif /* HAVE_FORK1 */
Guido van Rossum2242f2f2001-04-11 20:58:20 +00006230
6231
Guido van Rossumad0ee831995-03-01 10:34:45 +00006232#ifdef HAVE_FORK
Larry Hastings2f936352014-08-05 14:04:04 +10006233/*[clinic input]
6234os.fork
6235
6236Fork a child process.
6237
6238Return 0 to child process and PID of child to parent process.
6239[clinic start generated code]*/
6240
Larry Hastings2f936352014-08-05 14:04:04 +10006241static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006242os_fork_impl(PyObject *module)
6243/*[clinic end generated code: output=3626c81f98985d49 input=13c956413110eeaa]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006244{
Victor Stinner8c62be82010-05-06 00:08:46 +00006245 pid_t pid;
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006246
Victor Stinner81a7be32020-04-14 15:14:01 +02006247 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006248 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6249 return NULL;
6250 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006251 if (PySys_Audit("os.fork", NULL) < 0) {
6252 return NULL;
6253 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006254 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006255 pid = fork();
6256 if (pid == 0) {
6257 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006258 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006259 } else {
6260 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006261 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006262 }
6263 if (pid == -1)
6264 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006265 return PyLong_FromPid(pid);
Guido van Rossum85e3b011991-06-03 12:42:10 +00006266}
Larry Hastings2f936352014-08-05 14:04:04 +10006267#endif /* HAVE_FORK */
6268
Guido van Rossum85e3b011991-06-03 12:42:10 +00006269
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006270#ifdef HAVE_SCHED_H
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006271#ifdef HAVE_SCHED_GET_PRIORITY_MAX
Larry Hastings2f936352014-08-05 14:04:04 +10006272/*[clinic input]
6273os.sched_get_priority_max
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006274
Larry Hastings2f936352014-08-05 14:04:04 +10006275 policy: int
6276
6277Get the maximum scheduling priority for policy.
6278[clinic start generated code]*/
6279
Larry Hastings2f936352014-08-05 14:04:04 +10006280static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006281os_sched_get_priority_max_impl(PyObject *module, int policy)
6282/*[clinic end generated code: output=9e465c6e43130521 input=2097b7998eca6874]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006283{
6284 int max;
6285
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006286 max = sched_get_priority_max(policy);
6287 if (max < 0)
6288 return posix_error();
6289 return PyLong_FromLong(max);
6290}
6291
Larry Hastings2f936352014-08-05 14:04:04 +10006292
6293/*[clinic input]
6294os.sched_get_priority_min
6295
6296 policy: int
6297
6298Get the minimum scheduling priority for policy.
6299[clinic start generated code]*/
6300
Larry Hastings2f936352014-08-05 14:04:04 +10006301static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006302os_sched_get_priority_min_impl(PyObject *module, int policy)
6303/*[clinic end generated code: output=7595c1138cc47a6d input=21bc8fa0d70983bf]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006304{
6305 int min = sched_get_priority_min(policy);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006306 if (min < 0)
6307 return posix_error();
6308 return PyLong_FromLong(min);
6309}
Charles-François Nataliea0d5fc2011-09-06 19:03:35 +02006310#endif /* HAVE_SCHED_GET_PRIORITY_MAX */
6311
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006312
Larry Hastings2f936352014-08-05 14:04:04 +10006313#ifdef HAVE_SCHED_SETSCHEDULER
6314/*[clinic input]
6315os.sched_getscheduler
6316 pid: pid_t
6317 /
6318
Min ho Kimc4cacc82019-07-31 08:16:13 +10006319Get the scheduling policy for the process identified by pid.
Larry Hastings2f936352014-08-05 14:04:04 +10006320
6321Passing 0 for pid returns the scheduling policy for the calling process.
6322[clinic start generated code]*/
6323
Larry Hastings2f936352014-08-05 14:04:04 +10006324static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006325os_sched_getscheduler_impl(PyObject *module, pid_t pid)
Min ho Kimc4cacc82019-07-31 08:16:13 +10006326/*[clinic end generated code: output=dce4c0bd3f1b34c8 input=8d99dac505485ac8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006327{
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006328 int policy;
6329
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006330 policy = sched_getscheduler(pid);
6331 if (policy < 0)
6332 return posix_error();
6333 return PyLong_FromLong(policy);
6334}
Larry Hastings2f936352014-08-05 14:04:04 +10006335#endif /* HAVE_SCHED_SETSCHEDULER */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006336
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006337
William Orr81574b82018-10-01 22:19:56 -07006338#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Larry Hastings2f936352014-08-05 14:04:04 +10006339/*[clinic input]
Eddie Elizondo474eedf2018-11-13 04:09:31 -08006340class os.sched_param "PyObject *" "SchedParamType"
Larry Hastings2f936352014-08-05 14:04:04 +10006341
6342@classmethod
6343os.sched_param.__new__
6344
6345 sched_priority: object
6346 A scheduling parameter.
6347
Eddie Elizondob3966632019-11-05 07:16:14 -08006348Currently has only one field: sched_priority
Larry Hastings2f936352014-08-05 14:04:04 +10006349[clinic start generated code]*/
6350
Larry Hastings2f936352014-08-05 14:04:04 +10006351static PyObject *
6352os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority)
Eddie Elizondob3966632019-11-05 07:16:14 -08006353/*[clinic end generated code: output=48f4067d60f48c13 input=eb42909a2c0e3e6c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006354{
6355 PyObject *res;
6356
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006357 res = PyStructSequence_New(type);
6358 if (!res)
6359 return NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10006360 Py_INCREF(sched_priority);
6361 PyStructSequence_SET_ITEM(res, 0, sched_priority);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006362 return res;
6363}
6364
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03006365PyDoc_VAR(os_sched_param__doc__);
6366
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006367static PyStructSequence_Field sched_param_fields[] = {
6368 {"sched_priority", "the scheduling priority"},
6369 {0}
6370};
6371
6372static PyStructSequence_Desc sched_param_desc = {
6373 "sched_param", /* name */
Larry Hastings2f936352014-08-05 14:04:04 +10006374 os_sched_param__doc__, /* doc */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006375 sched_param_fields,
6376 1
6377};
6378
6379static int
6380convert_sched_param(PyObject *param, struct sched_param *res)
6381{
6382 long priority;
6383
Andy Lester55728702020-03-06 16:53:17 -06006384 if (!Py_IS_TYPE(param, (PyTypeObject *)_posixstate_global->SchedParamType)) {
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006385 PyErr_SetString(PyExc_TypeError, "must have a sched_param object");
6386 return 0;
6387 }
6388 priority = PyLong_AsLong(PyStructSequence_GET_ITEM(param, 0));
6389 if (priority == -1 && PyErr_Occurred())
6390 return 0;
6391 if (priority > INT_MAX || priority < INT_MIN) {
6392 PyErr_SetString(PyExc_OverflowError, "sched_priority out of range");
6393 return 0;
6394 }
6395 res->sched_priority = Py_SAFE_DOWNCAST(priority, long, int);
6396 return 1;
6397}
William Orr81574b82018-10-01 22:19:56 -07006398#endif /* defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM) */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006399
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006400
6401#ifdef HAVE_SCHED_SETSCHEDULER
Larry Hastings2f936352014-08-05 14:04:04 +10006402/*[clinic input]
6403os.sched_setscheduler
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006404
Larry Hastings2f936352014-08-05 14:04:04 +10006405 pid: pid_t
6406 policy: int
6407 param: sched_param
6408 /
6409
6410Set the scheduling policy for the process identified by pid.
6411
6412If pid is 0, the calling process is changed.
6413param is an instance of sched_param.
6414[clinic start generated code]*/
6415
Larry Hastings2f936352014-08-05 14:04:04 +10006416static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006417os_sched_setscheduler_impl(PyObject *module, pid_t pid, int policy,
Larry Hastings89964c42015-04-14 18:07:59 -04006418 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006419/*[clinic end generated code: output=b0ac0a70d3b1d705 input=c581f9469a5327dd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006420{
Jesus Cea9c822272011-09-10 01:40:52 +02006421 /*
Jesus Cea54b01492011-09-10 01:53:19 +02006422 ** sched_setscheduler() returns 0 in Linux, but the previous
6423 ** scheduling policy under Solaris/Illumos, and others.
6424 ** On error, -1 is returned in all Operating Systems.
Jesus Cea9c822272011-09-10 01:40:52 +02006425 */
Larry Hastings2f936352014-08-05 14:04:04 +10006426 if (sched_setscheduler(pid, policy, param) == -1)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006427 return posix_error();
6428 Py_RETURN_NONE;
6429}
Larry Hastings2f936352014-08-05 14:04:04 +10006430#endif /* HAVE_SCHED_SETSCHEDULER*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006431
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006432
6433#ifdef HAVE_SCHED_SETPARAM
Larry Hastings2f936352014-08-05 14:04:04 +10006434/*[clinic input]
6435os.sched_getparam
6436 pid: pid_t
6437 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006438
Larry Hastings2f936352014-08-05 14:04:04 +10006439Returns scheduling parameters for the process identified by pid.
6440
6441If pid is 0, returns parameters for the calling process.
6442Return value is an instance of sched_param.
6443[clinic start generated code]*/
6444
Larry Hastings2f936352014-08-05 14:04:04 +10006445static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006446os_sched_getparam_impl(PyObject *module, pid_t pid)
6447/*[clinic end generated code: output=b194e8708dcf2db8 input=18a1ef9c2efae296]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006448{
6449 struct sched_param param;
6450 PyObject *result;
6451 PyObject *priority;
6452
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006453 if (sched_getparam(pid, &param))
6454 return posix_error();
Eddie Elizondob3966632019-11-05 07:16:14 -08006455 PyObject *SchedParamType = _posixstate_global->SchedParamType;
6456 result = PyStructSequence_New((PyTypeObject *)SchedParamType);
Larry Hastings2f936352014-08-05 14:04:04 +10006457 if (!result)
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006458 return NULL;
6459 priority = PyLong_FromLong(param.sched_priority);
6460 if (!priority) {
Larry Hastings2f936352014-08-05 14:04:04 +10006461 Py_DECREF(result);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006462 return NULL;
6463 }
Larry Hastings2f936352014-08-05 14:04:04 +10006464 PyStructSequence_SET_ITEM(result, 0, priority);
6465 return result;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006466}
6467
Larry Hastings2f936352014-08-05 14:04:04 +10006468
6469/*[clinic input]
6470os.sched_setparam
6471 pid: pid_t
6472 param: sched_param
6473 /
6474
6475Set scheduling parameters for the process identified by pid.
6476
6477If pid is 0, sets parameters for the calling process.
6478param should be an instance of sched_param.
6479[clinic start generated code]*/
6480
Larry Hastings2f936352014-08-05 14:04:04 +10006481static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006482os_sched_setparam_impl(PyObject *module, pid_t pid,
Larry Hastings89964c42015-04-14 18:07:59 -04006483 struct sched_param *param)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006484/*[clinic end generated code: output=8af013f78a32b591 input=6b8d6dfcecdc21bd]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006485{
6486 if (sched_setparam(pid, param))
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006487 return posix_error();
6488 Py_RETURN_NONE;
6489}
Larry Hastings2f936352014-08-05 14:04:04 +10006490#endif /* HAVE_SCHED_SETPARAM */
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006491
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006492
6493#ifdef HAVE_SCHED_RR_GET_INTERVAL
Larry Hastings2f936352014-08-05 14:04:04 +10006494/*[clinic input]
6495os.sched_rr_get_interval -> double
6496 pid: pid_t
6497 /
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006498
Larry Hastings2f936352014-08-05 14:04:04 +10006499Return the round-robin quantum for the process identified by pid, in seconds.
6500
6501Value returned is a float.
6502[clinic start generated code]*/
6503
Larry Hastings2f936352014-08-05 14:04:04 +10006504static double
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006505os_sched_rr_get_interval_impl(PyObject *module, pid_t pid)
6506/*[clinic end generated code: output=7e2d935833ab47dc input=2a973da15cca6fae]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006507{
6508 struct timespec interval;
6509 if (sched_rr_get_interval(pid, &interval)) {
6510 posix_error();
6511 return -1.0;
6512 }
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08006513#ifdef _Py_MEMORY_SANITIZER
6514 __msan_unpoison(&interval, sizeof(interval));
6515#endif
Larry Hastings2f936352014-08-05 14:04:04 +10006516 return (double)interval.tv_sec + 1e-9*interval.tv_nsec;
6517}
6518#endif /* HAVE_SCHED_RR_GET_INTERVAL */
Benjamin Petersonc5fce4d2011-08-02 18:07:32 -05006519
Larry Hastings2f936352014-08-05 14:04:04 +10006520
6521/*[clinic input]
6522os.sched_yield
6523
6524Voluntarily relinquish the CPU.
6525[clinic start generated code]*/
6526
Larry Hastings2f936352014-08-05 14:04:04 +10006527static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006528os_sched_yield_impl(PyObject *module)
6529/*[clinic end generated code: output=902323500f222cac input=e54d6f98189391d4]*/
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006530{
6531 if (sched_yield())
6532 return posix_error();
6533 Py_RETURN_NONE;
6534}
6535
Benjamin Peterson2740af82011-08-02 17:41:34 -05006536#ifdef HAVE_SCHED_SETAFFINITY
Antoine Pitrou84869872012-08-04 16:16:35 +02006537/* The minimum number of CPUs allocated in a cpu_set_t */
6538static const int NCPUS_START = sizeof(unsigned long) * CHAR_BIT;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006539
Larry Hastings2f936352014-08-05 14:04:04 +10006540/*[clinic input]
6541os.sched_setaffinity
6542 pid: pid_t
6543 mask : object
6544 /
6545
6546Set the CPU affinity of the process identified by pid to mask.
6547
6548mask should be an iterable of integers identifying CPUs.
6549[clinic start generated code]*/
6550
Larry Hastings2f936352014-08-05 14:04:04 +10006551static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006552os_sched_setaffinity_impl(PyObject *module, pid_t pid, PyObject *mask)
6553/*[clinic end generated code: output=882d7dd9a229335b input=a0791a597c7085ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006554{
Antoine Pitrou84869872012-08-04 16:16:35 +02006555 int ncpus;
6556 size_t setsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006557 cpu_set_t *cpu_set = NULL;
6558 PyObject *iterator = NULL, *item;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006559
Larry Hastings2f936352014-08-05 14:04:04 +10006560 iterator = PyObject_GetIter(mask);
Antoine Pitrou84869872012-08-04 16:16:35 +02006561 if (iterator == NULL)
6562 return NULL;
6563
6564 ncpus = NCPUS_START;
6565 setsize = CPU_ALLOC_SIZE(ncpus);
Larry Hastings2f936352014-08-05 14:04:04 +10006566 cpu_set = CPU_ALLOC(ncpus);
6567 if (cpu_set == NULL) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006568 PyErr_NoMemory();
6569 goto error;
6570 }
Larry Hastings2f936352014-08-05 14:04:04 +10006571 CPU_ZERO_S(setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006572
6573 while ((item = PyIter_Next(iterator))) {
6574 long cpu;
6575 if (!PyLong_Check(item)) {
6576 PyErr_Format(PyExc_TypeError,
6577 "expected an iterator of ints, "
6578 "but iterator yielded %R",
6579 Py_TYPE(item));
6580 Py_DECREF(item);
6581 goto error;
6582 }
6583 cpu = PyLong_AsLong(item);
6584 Py_DECREF(item);
6585 if (cpu < 0) {
6586 if (!PyErr_Occurred())
6587 PyErr_SetString(PyExc_ValueError, "negative CPU number");
6588 goto error;
6589 }
6590 if (cpu > INT_MAX - 1) {
6591 PyErr_SetString(PyExc_OverflowError, "CPU number too large");
6592 goto error;
6593 }
6594 if (cpu >= ncpus) {
6595 /* Grow CPU mask to fit the CPU number */
6596 int newncpus = ncpus;
6597 cpu_set_t *newmask;
6598 size_t newsetsize;
6599 while (newncpus <= cpu) {
6600 if (newncpus > INT_MAX / 2)
6601 newncpus = cpu + 1;
6602 else
6603 newncpus = newncpus * 2;
6604 }
6605 newmask = CPU_ALLOC(newncpus);
6606 if (newmask == NULL) {
6607 PyErr_NoMemory();
6608 goto error;
6609 }
6610 newsetsize = CPU_ALLOC_SIZE(newncpus);
6611 CPU_ZERO_S(newsetsize, newmask);
Larry Hastings2f936352014-08-05 14:04:04 +10006612 memcpy(newmask, cpu_set, setsize);
6613 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006614 setsize = newsetsize;
Larry Hastings2f936352014-08-05 14:04:04 +10006615 cpu_set = newmask;
Antoine Pitrou84869872012-08-04 16:16:35 +02006616 ncpus = newncpus;
6617 }
Larry Hastings2f936352014-08-05 14:04:04 +10006618 CPU_SET_S(cpu, setsize, cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006619 }
Brandt Bucher45a30af2019-06-27 09:10:57 -07006620 if (PyErr_Occurred()) {
6621 goto error;
6622 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006623 Py_CLEAR(iterator);
6624
Larry Hastings2f936352014-08-05 14:04:04 +10006625 if (sched_setaffinity(pid, setsize, cpu_set)) {
Antoine Pitrou84869872012-08-04 16:16:35 +02006626 posix_error();
6627 goto error;
6628 }
Larry Hastings2f936352014-08-05 14:04:04 +10006629 CPU_FREE(cpu_set);
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006630 Py_RETURN_NONE;
Antoine Pitrou84869872012-08-04 16:16:35 +02006631
6632error:
Larry Hastings2f936352014-08-05 14:04:04 +10006633 if (cpu_set)
6634 CPU_FREE(cpu_set);
Antoine Pitrou84869872012-08-04 16:16:35 +02006635 Py_XDECREF(iterator);
6636 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006637}
6638
Larry Hastings2f936352014-08-05 14:04:04 +10006639
6640/*[clinic input]
6641os.sched_getaffinity
6642 pid: pid_t
6643 /
6644
Charles-François Natalidc87e4b2015-07-13 21:01:39 +01006645Return the affinity of the process identified by pid (or the current process if zero).
Larry Hastings2f936352014-08-05 14:04:04 +10006646
6647The affinity is returned as a set of CPU identifiers.
6648[clinic start generated code]*/
6649
Larry Hastings2f936352014-08-05 14:04:04 +10006650static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006651os_sched_getaffinity_impl(PyObject *module, pid_t pid)
Serhiy Storchaka2954f832016-07-07 18:20:03 +03006652/*[clinic end generated code: output=f726f2c193c17a4f input=983ce7cb4a565980]*/
Larry Hastings2f936352014-08-05 14:04:04 +10006653{
Antoine Pitrou84869872012-08-04 16:16:35 +02006654 int cpu, ncpus, count;
6655 size_t setsize;
6656 cpu_set_t *mask = NULL;
6657 PyObject *res = NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006658
Antoine Pitrou84869872012-08-04 16:16:35 +02006659 ncpus = NCPUS_START;
6660 while (1) {
6661 setsize = CPU_ALLOC_SIZE(ncpus);
6662 mask = CPU_ALLOC(ncpus);
6663 if (mask == NULL)
6664 return PyErr_NoMemory();
6665 if (sched_getaffinity(pid, setsize, mask) == 0)
6666 break;
6667 CPU_FREE(mask);
6668 if (errno != EINVAL)
6669 return posix_error();
6670 if (ncpus > INT_MAX / 2) {
6671 PyErr_SetString(PyExc_OverflowError, "could not allocate "
6672 "a large enough CPU set");
6673 return NULL;
6674 }
6675 ncpus = ncpus * 2;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006676 }
Antoine Pitrou84869872012-08-04 16:16:35 +02006677
6678 res = PySet_New(NULL);
6679 if (res == NULL)
6680 goto error;
6681 for (cpu = 0, count = CPU_COUNT_S(setsize, mask); count; cpu++) {
6682 if (CPU_ISSET_S(cpu, setsize, mask)) {
6683 PyObject *cpu_num = PyLong_FromLong(cpu);
6684 --count;
6685 if (cpu_num == NULL)
6686 goto error;
6687 if (PySet_Add(res, cpu_num)) {
6688 Py_DECREF(cpu_num);
6689 goto error;
6690 }
6691 Py_DECREF(cpu_num);
6692 }
6693 }
6694 CPU_FREE(mask);
6695 return res;
6696
6697error:
6698 if (mask)
6699 CPU_FREE(mask);
6700 Py_XDECREF(res);
6701 return NULL;
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006702}
6703
Benjamin Peterson2740af82011-08-02 17:41:34 -05006704#endif /* HAVE_SCHED_SETAFFINITY */
6705
Benjamin Peterson94b580d2011-08-02 17:30:04 -05006706#endif /* HAVE_SCHED_H */
6707
Larry Hastings2f936352014-08-05 14:04:04 +10006708
Neal Norwitzb59798b2003-03-21 01:43:31 +00006709/* AIX uses /dev/ptc but is otherwise the same as /dev/ptmx */
Neal Norwitz2deaddb2003-03-21 03:08:31 +00006710/* IRIX has both /dev/ptc and /dev/ptmx, use ptmx */
6711#if defined(HAVE_DEV_PTC) && !defined(HAVE_DEV_PTMX)
Neal Norwitzb59798b2003-03-21 01:43:31 +00006712#define DEV_PTY_FILE "/dev/ptc"
6713#define HAVE_DEV_PTMX
6714#else
6715#define DEV_PTY_FILE "/dev/ptmx"
6716#endif
6717
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006718#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX)
Fred Drake8cef4cf2000-06-28 16:40:38 +00006719#ifdef HAVE_PTY_H
6720#include <pty.h>
6721#else
6722#ifdef HAVE_LIBUTIL_H
6723#include <libutil.h>
Ronald Oussoren755740f2010-02-07 19:56:39 +00006724#else
6725#ifdef HAVE_UTIL_H
6726#include <util.h>
6727#endif /* HAVE_UTIL_H */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006728#endif /* HAVE_LIBUTIL_H */
6729#endif /* HAVE_PTY_H */
Martin v. Löwis14e73b12003-01-01 09:51:12 +00006730#ifdef HAVE_STROPTS_H
6731#include <stropts.h>
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006732#endif
ngie-eign7745ec42018-02-14 11:54:28 -08006733#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006734
Larry Hastings2f936352014-08-05 14:04:04 +10006735
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006736#if defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX)
Larry Hastings2f936352014-08-05 14:04:04 +10006737/*[clinic input]
6738os.openpty
6739
6740Open a pseudo-terminal.
6741
6742Return a tuple of (master_fd, slave_fd) containing open file descriptors
6743for both the master and slave ends.
6744[clinic start generated code]*/
6745
Larry Hastings2f936352014-08-05 14:04:04 +10006746static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006747os_openpty_impl(PyObject *module)
6748/*[clinic end generated code: output=98841ce5ec9cef3c input=f3d99fd99e762907]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006749{
Victor Stinnerdaf45552013-08-28 00:53:59 +02006750 int master_fd = -1, slave_fd = -1;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006751#ifndef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006752 char * slave_name;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006753#endif
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006754#if defined(HAVE_DEV_PTMX) && !defined(HAVE_OPENPTY) && !defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006755 PyOS_sighandler_t sig_saved;
Jakub Kulík6f9bc722018-12-31 03:16:40 +01006756#if defined(__sun) && defined(__SVR4)
Victor Stinner8c62be82010-05-06 00:08:46 +00006757 extern char *ptsname(int fildes);
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006758#endif
6759#endif
Thomas Wouters70c21a12000-07-14 14:28:33 +00006760
Thomas Wouters70c21a12000-07-14 14:28:33 +00006761#ifdef HAVE_OPENPTY
Victor Stinner8c62be82010-05-06 00:08:46 +00006762 if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) != 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006763 goto posix_error;
6764
6765 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6766 goto error;
6767 if (_Py_set_inheritable(slave_fd, 0, NULL) < 0)
6768 goto error;
6769
Neal Norwitzb59798b2003-03-21 01:43:31 +00006770#elif defined(HAVE__GETPTY)
Victor Stinner8c62be82010-05-06 00:08:46 +00006771 slave_name = _getpty(&master_fd, O_RDWR, 0666, 0);
6772 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006773 goto posix_error;
6774 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6775 goto error;
Thomas Wouters70c21a12000-07-14 14:28:33 +00006776
Victor Stinnerdaf45552013-08-28 00:53:59 +02006777 slave_fd = _Py_open(slave_name, O_RDWR);
Victor Stinner8c62be82010-05-06 00:08:46 +00006778 if (slave_fd < 0)
Victor Stinnera555cfc2015-03-18 00:22:14 +01006779 goto error;
Victor Stinnerdaf45552013-08-28 00:53:59 +02006780
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006781#else
Victor Stinner000de532013-11-25 23:19:58 +01006782 master_fd = open(DEV_PTY_FILE, O_RDWR | O_NOCTTY); /* open master */
Victor Stinner8c62be82010-05-06 00:08:46 +00006783 if (master_fd < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006784 goto posix_error;
6785
Victor Stinner8c62be82010-05-06 00:08:46 +00006786 sig_saved = PyOS_setsig(SIGCHLD, SIG_DFL);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006787
Victor Stinner8c62be82010-05-06 00:08:46 +00006788 /* change permission of slave */
6789 if (grantpt(master_fd) < 0) {
6790 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006791 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006792 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006793
Victor Stinner8c62be82010-05-06 00:08:46 +00006794 /* unlock slave */
6795 if (unlockpt(master_fd) < 0) {
6796 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006797 goto posix_error;
Victor Stinner8c62be82010-05-06 00:08:46 +00006798 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02006799
Victor Stinner8c62be82010-05-06 00:08:46 +00006800 PyOS_setsig(SIGCHLD, sig_saved);
Victor Stinnerdaf45552013-08-28 00:53:59 +02006801
Victor Stinner8c62be82010-05-06 00:08:46 +00006802 slave_name = ptsname(master_fd); /* get name of slave */
6803 if (slave_name == NULL)
Victor Stinnerdaf45552013-08-28 00:53:59 +02006804 goto posix_error;
6805
6806 slave_fd = _Py_open(slave_name, O_RDWR | O_NOCTTY); /* open slave */
Victor Stinnera555cfc2015-03-18 00:22:14 +01006807 if (slave_fd == -1)
6808 goto error;
Victor Stinner000de532013-11-25 23:19:58 +01006809
6810 if (_Py_set_inheritable(master_fd, 0, NULL) < 0)
6811 goto posix_error;
6812
Stefan Krahfb7c8ae2016-04-26 17:04:18 +02006813#if !defined(__CYGWIN__) && !defined(__ANDROID__) && !defined(HAVE_DEV_PTC)
Victor Stinner8c62be82010-05-06 00:08:46 +00006814 ioctl(slave_fd, I_PUSH, "ptem"); /* push ptem */
6815 ioctl(slave_fd, I_PUSH, "ldterm"); /* push ldterm */
Neal Norwitz6700e472002-12-31 16:16:07 +00006816#ifndef __hpux
Victor Stinner8c62be82010-05-06 00:08:46 +00006817 ioctl(slave_fd, I_PUSH, "ttcompat"); /* push ttcompat */
Neal Norwitz6700e472002-12-31 16:16:07 +00006818#endif /* __hpux */
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006819#endif /* HAVE_CYGWIN */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +00006820#endif /* HAVE_OPENPTY */
Thomas Wouters70c21a12000-07-14 14:28:33 +00006821
Victor Stinner8c62be82010-05-06 00:08:46 +00006822 return Py_BuildValue("(ii)", master_fd, slave_fd);
Thomas Wouters70c21a12000-07-14 14:28:33 +00006823
Victor Stinnerdaf45552013-08-28 00:53:59 +02006824posix_error:
6825 posix_error();
6826error:
6827 if (master_fd != -1)
6828 close(master_fd);
6829 if (slave_fd != -1)
6830 close(slave_fd);
6831 return NULL;
Fred Drake8cef4cf2000-06-28 16:40:38 +00006832}
Martin v. Löwis24a880b2002-12-31 12:55:15 +00006833#endif /* defined(HAVE_OPENPTY) || defined(HAVE__GETPTY) || defined(HAVE_DEV_PTMX) */
Fred Drake8cef4cf2000-06-28 16:40:38 +00006834
Larry Hastings2f936352014-08-05 14:04:04 +10006835
Fred Drake8cef4cf2000-06-28 16:40:38 +00006836#ifdef HAVE_FORKPTY
Larry Hastings2f936352014-08-05 14:04:04 +10006837/*[clinic input]
6838os.forkpty
6839
6840Fork a new process with a new pseudo-terminal as controlling tty.
6841
6842Returns a tuple of (pid, master_fd).
6843Like fork(), return pid of 0 to the child process,
6844and pid of child to the parent process.
6845To both, return fd of newly opened pseudo-terminal.
6846[clinic start generated code]*/
6847
Larry Hastings2f936352014-08-05 14:04:04 +10006848static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006849os_forkpty_impl(PyObject *module)
6850/*[clinic end generated code: output=60d0a5c7512e4087 input=f1f7f4bae3966010]*/
Fred Drake8cef4cf2000-06-28 16:40:38 +00006851{
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006852 int master_fd = -1;
Victor Stinner8c62be82010-05-06 00:08:46 +00006853 pid_t pid;
Tim Peters5aa91602002-01-30 05:46:57 +00006854
Victor Stinner81a7be32020-04-14 15:14:01 +02006855 if (_PyInterpreterState_GET() != PyInterpreterState_Main()) {
Eric Snow59032962018-09-14 14:17:20 -07006856 PyErr_SetString(PyExc_RuntimeError, "fork not supported for subinterpreters");
6857 return NULL;
6858 }
Saiyang Gou7514f4f2020-02-12 23:47:42 -08006859 if (PySys_Audit("os.forkpty", NULL) < 0) {
6860 return NULL;
6861 }
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006862 PyOS_BeforeFork();
Victor Stinner8c62be82010-05-06 00:08:46 +00006863 pid = forkpty(&master_fd, NULL, NULL, NULL);
6864 if (pid == 0) {
6865 /* child: this clobbers and resets the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006866 PyOS_AfterFork_Child();
Victor Stinner8c62be82010-05-06 00:08:46 +00006867 } else {
6868 /* parent: release the import lock. */
Antoine Pitrou346cbd32017-05-27 17:50:54 +02006869 PyOS_AfterFork_Parent();
Victor Stinner8c62be82010-05-06 00:08:46 +00006870 }
6871 if (pid == -1)
6872 return posix_error();
Victor Stinner8c62be82010-05-06 00:08:46 +00006873 return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd);
Fred Drake8cef4cf2000-06-28 16:40:38 +00006874}
Larry Hastings2f936352014-08-05 14:04:04 +10006875#endif /* HAVE_FORKPTY */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006876
Ross Lagerwall7807c352011-03-17 20:20:30 +02006877
Guido van Rossumad0ee831995-03-01 10:34:45 +00006878#ifdef HAVE_GETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10006879/*[clinic input]
6880os.getegid
6881
6882Return the current process's effective group id.
6883[clinic start generated code]*/
6884
Larry Hastings2f936352014-08-05 14:04:04 +10006885static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006886os_getegid_impl(PyObject *module)
6887/*[clinic end generated code: output=67d9be7ac68898a2 input=1596f79ad1107d5d]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006888{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006889 return _PyLong_FromGid(getegid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006890}
Larry Hastings2f936352014-08-05 14:04:04 +10006891#endif /* HAVE_GETEGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006892
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006893
Guido van Rossumad0ee831995-03-01 10:34:45 +00006894#ifdef HAVE_GETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10006895/*[clinic input]
6896os.geteuid
6897
6898Return the current process's effective user id.
6899[clinic start generated code]*/
6900
Larry Hastings2f936352014-08-05 14:04:04 +10006901static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006902os_geteuid_impl(PyObject *module)
6903/*[clinic end generated code: output=ea1b60f0d6abb66e input=4644c662d3bd9f19]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006904{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006905 return _PyLong_FromUid(geteuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006906}
Larry Hastings2f936352014-08-05 14:04:04 +10006907#endif /* HAVE_GETEUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006908
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006909
Guido van Rossumad0ee831995-03-01 10:34:45 +00006910#ifdef HAVE_GETGID
Larry Hastings2f936352014-08-05 14:04:04 +10006911/*[clinic input]
6912os.getgid
6913
6914Return the current process's group id.
6915[clinic start generated code]*/
6916
Larry Hastings2f936352014-08-05 14:04:04 +10006917static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006918os_getgid_impl(PyObject *module)
6919/*[clinic end generated code: output=4f28ebc9d3e5dfcf input=58796344cd87c0f6]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00006920{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02006921 return _PyLong_FromGid(getgid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00006922}
Larry Hastings2f936352014-08-05 14:04:04 +10006923#endif /* HAVE_GETGID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00006924
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00006925
Berker Peksag39404992016-09-15 20:45:16 +03006926#ifdef HAVE_GETPID
Larry Hastings2f936352014-08-05 14:04:04 +10006927/*[clinic input]
6928os.getpid
6929
6930Return the current process id.
6931[clinic start generated code]*/
6932
Larry Hastings2f936352014-08-05 14:04:04 +10006933static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03006934os_getpid_impl(PyObject *module)
6935/*[clinic end generated code: output=9ea6fdac01ed2b3c input=5a9a00f0ab68aa00]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00006936{
Victor Stinner8c62be82010-05-06 00:08:46 +00006937 return PyLong_FromPid(getpid());
Guido van Rossum85e3b011991-06-03 12:42:10 +00006938}
Berker Peksag39404992016-09-15 20:45:16 +03006939#endif /* HAVE_GETPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00006940
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006941#ifdef NGROUPS_MAX
6942#define MAX_GROUPS NGROUPS_MAX
6943#else
6944 /* defined to be 16 on Solaris7, so this should be a small number */
6945#define MAX_GROUPS 64
6946#endif
6947
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006948#ifdef HAVE_GETGROUPLIST
Larry Hastings2f936352014-08-05 14:04:04 +10006949
Serhiy Storchaka2b560312020-04-18 19:14:10 +03006950#ifdef __APPLE__
6951/*[clinic input]
6952os.getgrouplist
6953
6954 user: str
6955 username to lookup
6956 group as basegid: int
6957 base group id of the user
6958 /
6959
6960Returns a list of groups to which a user belongs.
6961[clinic start generated code]*/
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006962
6963static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03006964os_getgrouplist_impl(PyObject *module, const char *user, int basegid)
6965/*[clinic end generated code: output=6e734697b8c26de0 input=f8d870374b09a490]*/
6966#else
6967/*[clinic input]
6968os.getgrouplist
6969
6970 user: str
6971 username to lookup
6972 group as basegid: gid_t
6973 base group id of the user
6974 /
6975
6976Returns a list of groups to which a user belongs.
6977[clinic start generated code]*/
6978
6979static PyObject *
6980os_getgrouplist_impl(PyObject *module, const char *user, gid_t basegid)
6981/*[clinic end generated code: output=0ebd7fb70115575b input=cc61d5c20b08958d]*/
6982#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006983{
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006984 int i, ngroups;
6985 PyObject *list;
6986#ifdef __APPLE__
Serhiy Storchaka2b560312020-04-18 19:14:10 +03006987 int *groups;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006988#else
Serhiy Storchaka2b560312020-04-18 19:14:10 +03006989 gid_t *groups;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02006990#endif
Jeffrey Kintscher8725c832019-06-13 00:01:29 -07006991
6992 /*
6993 * NGROUPS_MAX is defined by POSIX.1 as the maximum
6994 * number of supplimental groups a users can belong to.
6995 * We have to increment it by one because
6996 * getgrouplist() returns both the supplemental groups
6997 * and the primary group, i.e. all of the groups the
6998 * user belongs to.
6999 */
7000 ngroups = 1 + MAX_GROUPS;
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007001
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007002 while (1) {
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007003#ifdef __APPLE__
Victor Stinner8ec73702020-03-23 20:00:57 +01007004 groups = PyMem_New(int, ngroups);
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007005#else
7006 groups = PyMem_New(gid_t, ngroups);
7007#endif
Victor Stinner8ec73702020-03-23 20:00:57 +01007008 if (groups == NULL) {
7009 return PyErr_NoMemory();
7010 }
Victor Stinnerf5c7cab2020-03-24 18:22:10 +01007011
7012 int old_ngroups = ngroups;
7013 if (getgrouplist(user, basegid, groups, &ngroups) != -1) {
7014 /* Success */
7015 break;
7016 }
7017
7018 /* getgrouplist() fails if the group list is too small */
7019 PyMem_Free(groups);
7020
7021 if (ngroups > old_ngroups) {
7022 /* If the group list is too small, the glibc implementation of
7023 getgrouplist() sets ngroups to the total number of groups and
7024 returns -1. */
7025 }
7026 else {
7027 /* Double the group list size */
7028 if (ngroups > INT_MAX / 2) {
7029 return PyErr_NoMemory();
7030 }
7031 ngroups *= 2;
7032 }
7033
7034 /* Retry getgrouplist() with a larger group list */
Victor Stinner8ec73702020-03-23 20:00:57 +01007035 }
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007036
Gregory P. Smith1d300ce2018-12-30 21:13:02 -08007037#ifdef _Py_MEMORY_SANITIZER
7038 /* Clang memory sanitizer libc intercepts don't know getgrouplist. */
7039 __msan_unpoison(&ngroups, sizeof(ngroups));
7040 __msan_unpoison(groups, ngroups*sizeof(*groups));
7041#endif
7042
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007043 list = PyList_New(ngroups);
7044 if (list == NULL) {
7045 PyMem_Del(groups);
7046 return NULL;
7047 }
7048
7049 for (i = 0; i < ngroups; i++) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007050#ifdef __APPLE__
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007051 PyObject *o = PyLong_FromUnsignedLong((unsigned long)groups[i]);
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007052#else
7053 PyObject *o = _PyLong_FromGid(groups[i]);
7054#endif
Ross Lagerwallb0ae53d2011-06-10 07:30:30 +02007055 if (o == NULL) {
7056 Py_DECREF(list);
7057 PyMem_Del(groups);
7058 return NULL;
7059 }
7060 PyList_SET_ITEM(list, i, o);
7061 }
7062
7063 PyMem_Del(groups);
7064
7065 return list;
7066}
Larry Hastings2f936352014-08-05 14:04:04 +10007067#endif /* HAVE_GETGROUPLIST */
7068
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007069
Fred Drakec9680921999-12-13 16:37:25 +00007070#ifdef HAVE_GETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007071/*[clinic input]
7072os.getgroups
7073
7074Return list of supplemental group IDs for the process.
7075[clinic start generated code]*/
7076
Larry Hastings2f936352014-08-05 14:04:04 +10007077static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007078os_getgroups_impl(PyObject *module)
7079/*[clinic end generated code: output=42b0c17758561b56 input=d3f109412e6a155c]*/
Fred Drakec9680921999-12-13 16:37:25 +00007080{
7081 PyObject *result = NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00007082 gid_t grouplist[MAX_GROUPS];
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007083
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007084 /* On MacOSX getgroups(2) can return more than MAX_GROUPS results
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007085 * This is a helper variable to store the intermediate result when
7086 * that happens.
7087 *
7088 * To keep the code readable the OSX behaviour is unconditional,
7089 * according to the POSIX spec this should be safe on all unix-y
7090 * systems.
7091 */
7092 gid_t* alt_grouplist = grouplist;
Victor Stinner8c62be82010-05-06 00:08:46 +00007093 int n;
Fred Drakec9680921999-12-13 16:37:25 +00007094
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007095#ifdef __APPLE__
7096 /* Issue #17557: As of OS X 10.8, getgroups(2) no longer raises EINVAL if
7097 * there are more groups than can fit in grouplist. Therefore, on OS X
7098 * always first call getgroups with length 0 to get the actual number
7099 * of groups.
7100 */
7101 n = getgroups(0, NULL);
7102 if (n < 0) {
7103 return posix_error();
7104 } else if (n <= MAX_GROUPS) {
7105 /* groups will fit in existing array */
7106 alt_grouplist = grouplist;
7107 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007108 alt_grouplist = PyMem_New(gid_t, n);
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007109 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007110 return PyErr_NoMemory();
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007111 }
7112 }
7113
7114 n = getgroups(n, alt_grouplist);
7115 if (n == -1) {
7116 if (alt_grouplist != grouplist) {
7117 PyMem_Free(alt_grouplist);
7118 }
7119 return posix_error();
7120 }
7121#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007122 n = getgroups(MAX_GROUPS, grouplist);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007123 if (n < 0) {
7124 if (errno == EINVAL) {
7125 n = getgroups(0, NULL);
7126 if (n == -1) {
7127 return posix_error();
7128 }
7129 if (n == 0) {
7130 /* Avoid malloc(0) */
7131 alt_grouplist = grouplist;
7132 } else {
Serhiy Storchaka1a1ff292015-02-16 13:28:22 +02007133 alt_grouplist = PyMem_New(gid_t, n);
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007134 if (alt_grouplist == NULL) {
Zackery Spytz4c49da02018-12-07 03:11:30 -07007135 return PyErr_NoMemory();
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007136 }
7137 n = getgroups(n, alt_grouplist);
7138 if (n == -1) {
7139 PyMem_Free(alt_grouplist);
7140 return posix_error();
7141 }
7142 }
7143 } else {
7144 return posix_error();
7145 }
7146 }
Ned Deilyb5dd6d22013-08-01 21:21:15 -07007147#endif
7148
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007149 result = PyList_New(n);
7150 if (result != NULL) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007151 int i;
7152 for (i = 0; i < n; ++i) {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007153 PyObject *o = _PyLong_FromGid(alt_grouplist[i]);
Victor Stinner8c62be82010-05-06 00:08:46 +00007154 if (o == NULL) {
Stefan Krah0e803b32010-11-26 16:16:47 +00007155 Py_DECREF(result);
7156 result = NULL;
7157 break;
Fred Drakec9680921999-12-13 16:37:25 +00007158 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007159 PyList_SET_ITEM(result, i, o);
Fred Drakec9680921999-12-13 16:37:25 +00007160 }
Ronald Oussorenb6ee4f52010-07-23 13:53:51 +00007161 }
7162
7163 if (alt_grouplist != grouplist) {
7164 PyMem_Free(alt_grouplist);
Victor Stinner8c62be82010-05-06 00:08:46 +00007165 }
Neal Norwitze241ce82003-02-17 18:17:05 +00007166
Fred Drakec9680921999-12-13 16:37:25 +00007167 return result;
7168}
Larry Hastings2f936352014-08-05 14:04:04 +10007169#endif /* HAVE_GETGROUPS */
Fred Drakec9680921999-12-13 16:37:25 +00007170
Antoine Pitroub7572f02009-12-02 20:46:48 +00007171#ifdef HAVE_INITGROUPS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007172#ifdef __APPLE__
7173/*[clinic input]
7174os.initgroups
Antoine Pitroub7572f02009-12-02 20:46:48 +00007175
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007176 username as oname: FSConverter
7177 gid: int
7178 /
7179
7180Initialize the group access list.
7181
7182Call the system initgroups() to initialize the group access list with all of
7183the groups of which the specified username is a member, plus the specified
7184group id.
7185[clinic start generated code]*/
7186
Antoine Pitroub7572f02009-12-02 20:46:48 +00007187static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007188os_initgroups_impl(PyObject *module, PyObject *oname, int gid)
7189/*[clinic end generated code: output=7f074d30a425fd3a input=df3d54331b0af204]*/
7190#else
7191/*[clinic input]
7192os.initgroups
7193
7194 username as oname: FSConverter
7195 gid: gid_t
7196 /
7197
7198Initialize the group access list.
7199
7200Call the system initgroups() to initialize the group access list with all of
7201the groups of which the specified username is a member, plus the specified
7202group id.
7203[clinic start generated code]*/
7204
7205static PyObject *
7206os_initgroups_impl(PyObject *module, PyObject *oname, gid_t gid)
7207/*[clinic end generated code: output=59341244521a9e3f input=0cb91bdc59a4c564]*/
7208#endif
Antoine Pitroub7572f02009-12-02 20:46:48 +00007209{
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007210 const char *username = PyBytes_AS_STRING(oname);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007211
Serhiy Storchaka2b560312020-04-18 19:14:10 +03007212 if (initgroups(username, gid) == -1)
Victor Stinner8c62be82010-05-06 00:08:46 +00007213 return PyErr_SetFromErrno(PyExc_OSError);
Antoine Pitroub7572f02009-12-02 20:46:48 +00007214
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007215 Py_RETURN_NONE;
Antoine Pitroub7572f02009-12-02 20:46:48 +00007216}
Larry Hastings2f936352014-08-05 14:04:04 +10007217#endif /* HAVE_INITGROUPS */
7218
Antoine Pitroub7572f02009-12-02 20:46:48 +00007219
Martin v. Löwis606edc12002-06-13 21:09:11 +00007220#ifdef HAVE_GETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10007221/*[clinic input]
7222os.getpgid
7223
7224 pid: pid_t
7225
7226Call the system call getpgid(), and return the result.
7227[clinic start generated code]*/
7228
Larry Hastings2f936352014-08-05 14:04:04 +10007229static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007230os_getpgid_impl(PyObject *module, pid_t pid)
7231/*[clinic end generated code: output=1db95a97be205d18 input=39d710ae3baaf1c7]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007232{
7233 pid_t pgid = getpgid(pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007234 if (pgid < 0)
7235 return posix_error();
7236 return PyLong_FromPid(pgid);
Martin v. Löwis606edc12002-06-13 21:09:11 +00007237}
7238#endif /* HAVE_GETPGID */
7239
7240
Guido van Rossumb6775db1994-08-01 11:34:53 +00007241#ifdef HAVE_GETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007242/*[clinic input]
7243os.getpgrp
7244
7245Return the current process group id.
7246[clinic start generated code]*/
7247
Larry Hastings2f936352014-08-05 14:04:04 +10007248static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007249os_getpgrp_impl(PyObject *module)
7250/*[clinic end generated code: output=c4fc381e51103cf3 input=6846fb2bb9a3705e]*/
Guido van Rossum04814471991-06-04 20:23:49 +00007251{
Guido van Rossumb6775db1994-08-01 11:34:53 +00007252#ifdef GETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007253 return PyLong_FromPid(getpgrp(0));
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007254#else /* GETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007255 return PyLong_FromPid(getpgrp());
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007256#endif /* GETPGRP_HAVE_ARG */
Guido van Rossum04814471991-06-04 20:23:49 +00007257}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007258#endif /* HAVE_GETPGRP */
Guido van Rossum04814471991-06-04 20:23:49 +00007259
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007260
Guido van Rossumb6775db1994-08-01 11:34:53 +00007261#ifdef HAVE_SETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10007262/*[clinic input]
7263os.setpgrp
7264
7265Make the current process the leader of its process group.
7266[clinic start generated code]*/
7267
Larry Hastings2f936352014-08-05 14:04:04 +10007268static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007269os_setpgrp_impl(PyObject *module)
7270/*[clinic end generated code: output=2554735b0a60f0a0 input=1f0619fcb5731e7e]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00007271{
Guido van Rossum64933891994-10-20 21:56:42 +00007272#ifdef SETPGRP_HAVE_ARG
Victor Stinner8c62be82010-05-06 00:08:46 +00007273 if (setpgrp(0, 0) < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007274#else /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007275 if (setpgrp() < 0)
Guido van Rossum64933891994-10-20 21:56:42 +00007276#endif /* SETPGRP_HAVE_ARG */
Victor Stinner8c62be82010-05-06 00:08:46 +00007277 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007278 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00007279}
Guido van Rossumb6775db1994-08-01 11:34:53 +00007280#endif /* HAVE_SETPGRP */
7281
Guido van Rossumad0ee831995-03-01 10:34:45 +00007282#ifdef HAVE_GETPPID
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007283
7284#ifdef MS_WINDOWS
7285#include <tlhelp32.h>
7286
7287static PyObject*
7288win32_getppid()
7289{
7290 HANDLE snapshot;
7291 pid_t mypid;
7292 PyObject* result = NULL;
7293 BOOL have_record;
7294 PROCESSENTRY32 pe;
7295
7296 mypid = getpid(); /* This function never fails */
7297
7298 snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
7299 if (snapshot == INVALID_HANDLE_VALUE)
7300 return PyErr_SetFromWindowsErr(GetLastError());
7301
7302 pe.dwSize = sizeof(pe);
7303 have_record = Process32First(snapshot, &pe);
7304 while (have_record) {
7305 if (mypid == (pid_t)pe.th32ProcessID) {
7306 /* We could cache the ulong value in a static variable. */
7307 result = PyLong_FromPid((pid_t)pe.th32ParentProcessID);
7308 break;
7309 }
7310
7311 have_record = Process32Next(snapshot, &pe);
7312 }
7313
7314 /* If our loop exits and our pid was not found (result will be NULL)
7315 * then GetLastError will return ERROR_NO_MORE_FILES. This is an
7316 * error anyway, so let's raise it. */
7317 if (!result)
7318 result = PyErr_SetFromWindowsErr(GetLastError());
7319
7320 CloseHandle(snapshot);
7321
7322 return result;
7323}
7324#endif /*MS_WINDOWS*/
7325
Larry Hastings2f936352014-08-05 14:04:04 +10007326
7327/*[clinic input]
7328os.getppid
7329
7330Return the parent's process id.
7331
7332If the parent process has already exited, Windows machines will still
7333return its id; others systems will return the id of the 'init' process (1).
7334[clinic start generated code]*/
7335
Larry Hastings2f936352014-08-05 14:04:04 +10007336static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007337os_getppid_impl(PyObject *module)
7338/*[clinic end generated code: output=43b2a946a8c603b4 input=e637cb87539c030e]*/
Guido van Rossum85e3b011991-06-03 12:42:10 +00007339{
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007340#ifdef MS_WINDOWS
7341 return win32_getppid();
7342#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007343 return PyLong_FromPid(getppid());
Guido van Rossumad0ee831995-03-01 10:34:45 +00007344#endif
Amaury Forgeot d'Arc4b6fdf32010-09-07 21:31:17 +00007345}
7346#endif /* HAVE_GETPPID */
Guido van Rossum85e3b011991-06-03 12:42:10 +00007347
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007348
Fred Drake12c6e2d1999-12-14 21:25:03 +00007349#ifdef HAVE_GETLOGIN
Larry Hastings2f936352014-08-05 14:04:04 +10007350/*[clinic input]
7351os.getlogin
7352
7353Return the actual login name.
7354[clinic start generated code]*/
7355
Larry Hastings2f936352014-08-05 14:04:04 +10007356static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007357os_getlogin_impl(PyObject *module)
7358/*[clinic end generated code: output=a32e66a7e5715dac input=2a21ab1e917163df]*/
Fred Drake12c6e2d1999-12-14 21:25:03 +00007359{
Victor Stinner8c62be82010-05-06 00:08:46 +00007360 PyObject *result = NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007361#ifdef MS_WINDOWS
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007362 wchar_t user_name[UNLEN + 1];
Victor Stinner63941882011-09-29 00:42:28 +02007363 DWORD num_chars = Py_ARRAY_LENGTH(user_name);
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007364
7365 if (GetUserNameW(user_name, &num_chars)) {
7366 /* num_chars is the number of unicode chars plus null terminator */
7367 result = PyUnicode_FromWideChar(user_name, num_chars - 1);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00007368 }
7369 else
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007370 result = PyErr_SetFromWindowsErr(GetLastError());
7371#else
Victor Stinner8c62be82010-05-06 00:08:46 +00007372 char *name;
7373 int old_errno = errno;
Fred Drake12c6e2d1999-12-14 21:25:03 +00007374
Victor Stinner8c62be82010-05-06 00:08:46 +00007375 errno = 0;
7376 name = getlogin();
7377 if (name == NULL) {
7378 if (errno)
Victor Stinnere039ffe2010-08-15 09:33:08 +00007379 posix_error();
Fred Drake12c6e2d1999-12-14 21:25:03 +00007380 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007381 PyErr_SetString(PyExc_OSError, "unable to determine login name");
Victor Stinner8c62be82010-05-06 00:08:46 +00007382 }
7383 else
Victor Stinnere039ffe2010-08-15 09:33:08 +00007384 result = PyUnicode_DecodeFSDefault(name);
Victor Stinner8c62be82010-05-06 00:08:46 +00007385 errno = old_errno;
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007386#endif
Fred Drake12c6e2d1999-12-14 21:25:03 +00007387 return result;
7388}
Brian Curtine8e4b3b2010-09-23 20:04:14 +00007389#endif /* HAVE_GETLOGIN */
Fred Drake12c6e2d1999-12-14 21:25:03 +00007390
Larry Hastings2f936352014-08-05 14:04:04 +10007391
Guido van Rossumad0ee831995-03-01 10:34:45 +00007392#ifdef HAVE_GETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007393/*[clinic input]
7394os.getuid
7395
7396Return the current process's user id.
7397[clinic start generated code]*/
7398
Larry Hastings2f936352014-08-05 14:04:04 +10007399static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007400os_getuid_impl(PyObject *module)
7401/*[clinic end generated code: output=415c0b401ebed11a input=b53c8b35f110a516]*/
Guido van Rossum46003ff1992-05-15 11:05:24 +00007402{
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007403 return _PyLong_FromUid(getuid());
Guido van Rossum46003ff1992-05-15 11:05:24 +00007404}
Larry Hastings2f936352014-08-05 14:04:04 +10007405#endif /* HAVE_GETUID */
Guido van Rossum46003ff1992-05-15 11:05:24 +00007406
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007407
Brian Curtineb24d742010-04-12 17:16:38 +00007408#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007409#define HAVE_KILL
7410#endif /* MS_WINDOWS */
7411
7412#ifdef HAVE_KILL
7413/*[clinic input]
7414os.kill
7415
7416 pid: pid_t
7417 signal: Py_ssize_t
7418 /
7419
7420Kill a process with a signal.
7421[clinic start generated code]*/
7422
Larry Hastings2f936352014-08-05 14:04:04 +10007423static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007424os_kill_impl(PyObject *module, pid_t pid, Py_ssize_t signal)
7425/*[clinic end generated code: output=8e346a6701c88568 input=61a36b86ca275ab9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007426{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007427 if (PySys_Audit("os.kill", "in", pid, signal) < 0) {
7428 return NULL;
7429 }
7430#ifndef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10007431 if (kill(pid, (int)signal) == -1)
7432 return posix_error();
7433 Py_RETURN_NONE;
Larry Hastings2f936352014-08-05 14:04:04 +10007434#else /* !MS_WINDOWS */
Amaury Forgeot d'Arc0a589c92010-05-15 20:35:12 +00007435 PyObject *result;
Larry Hastings2f936352014-08-05 14:04:04 +10007436 DWORD sig = (DWORD)signal;
7437 DWORD err;
Victor Stinner8c62be82010-05-06 00:08:46 +00007438 HANDLE handle;
Brian Curtineb24d742010-04-12 17:16:38 +00007439
Victor Stinner8c62be82010-05-06 00:08:46 +00007440 /* Console processes which share a common console can be sent CTRL+C or
7441 CTRL+BREAK events, provided they handle said events. */
7442 if (sig == CTRL_C_EVENT || sig == CTRL_BREAK_EVENT) {
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007443 if (GenerateConsoleCtrlEvent(sig, (DWORD)pid) == 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007444 err = GetLastError();
7445 PyErr_SetFromWindowsErr(err);
7446 }
7447 else
7448 Py_RETURN_NONE;
7449 }
Brian Curtineb24d742010-04-12 17:16:38 +00007450
Victor Stinner8c62be82010-05-06 00:08:46 +00007451 /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
7452 attempt to open and terminate the process. */
Richard Oudkerkac0ad882013-06-05 23:29:30 +01007453 handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, (DWORD)pid);
Victor Stinner8c62be82010-05-06 00:08:46 +00007454 if (handle == NULL) {
7455 err = GetLastError();
7456 return PyErr_SetFromWindowsErr(err);
7457 }
Brian Curtineb24d742010-04-12 17:16:38 +00007458
Victor Stinner8c62be82010-05-06 00:08:46 +00007459 if (TerminateProcess(handle, sig) == 0) {
7460 err = GetLastError();
7461 result = PyErr_SetFromWindowsErr(err);
7462 } else {
7463 Py_INCREF(Py_None);
7464 result = Py_None;
7465 }
Brian Curtineb24d742010-04-12 17:16:38 +00007466
Victor Stinner8c62be82010-05-06 00:08:46 +00007467 CloseHandle(handle);
7468 return result;
Larry Hastings2f936352014-08-05 14:04:04 +10007469#endif /* !MS_WINDOWS */
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007470}
Larry Hastings2f936352014-08-05 14:04:04 +10007471#endif /* HAVE_KILL */
7472
7473
7474#ifdef HAVE_KILLPG
7475/*[clinic input]
7476os.killpg
7477
7478 pgid: pid_t
7479 signal: int
7480 /
7481
7482Kill a process group with a signal.
7483[clinic start generated code]*/
7484
Larry Hastings2f936352014-08-05 14:04:04 +10007485static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007486os_killpg_impl(PyObject *module, pid_t pgid, int signal)
7487/*[clinic end generated code: output=6dbcd2f1fdf5fdba input=38b5449eb8faec19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007488{
Saiyang Gou7514f4f2020-02-12 23:47:42 -08007489 if (PySys_Audit("os.killpg", "ii", pgid, signal) < 0) {
7490 return NULL;
7491 }
Larry Hastings2f936352014-08-05 14:04:04 +10007492 /* XXX some man pages make the `pgid` parameter an int, others
7493 a pid_t. Since getpgrp() returns a pid_t, we assume killpg should
7494 take the same type. Moreover, pid_t is always at least as wide as
7495 int (else compilation of this module fails), which is safe. */
7496 if (killpg(pgid, signal) == -1)
7497 return posix_error();
7498 Py_RETURN_NONE;
7499}
7500#endif /* HAVE_KILLPG */
7501
Brian Curtineb24d742010-04-12 17:16:38 +00007502
Guido van Rossumc0125471996-06-28 18:55:32 +00007503#ifdef HAVE_PLOCK
Guido van Rossumc0125471996-06-28 18:55:32 +00007504#ifdef HAVE_SYS_LOCK_H
7505#include <sys/lock.h>
7506#endif
7507
Larry Hastings2f936352014-08-05 14:04:04 +10007508/*[clinic input]
7509os.plock
7510 op: int
7511 /
7512
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007513Lock program segments into memory.");
Larry Hastings2f936352014-08-05 14:04:04 +10007514[clinic start generated code]*/
7515
Larry Hastings2f936352014-08-05 14:04:04 +10007516static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007517os_plock_impl(PyObject *module, int op)
7518/*[clinic end generated code: output=81424167033b168e input=e6e5e348e1525f60]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007519{
Victor Stinner8c62be82010-05-06 00:08:46 +00007520 if (plock(op) == -1)
7521 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007522 Py_RETURN_NONE;
Guido van Rossumc0125471996-06-28 18:55:32 +00007523}
Larry Hastings2f936352014-08-05 14:04:04 +10007524#endif /* HAVE_PLOCK */
7525
Guido van Rossumc0125471996-06-28 18:55:32 +00007526
Guido van Rossumb6775db1994-08-01 11:34:53 +00007527#ifdef HAVE_SETUID
Larry Hastings2f936352014-08-05 14:04:04 +10007528/*[clinic input]
7529os.setuid
7530
7531 uid: uid_t
7532 /
7533
7534Set the current process's user id.
7535[clinic start generated code]*/
7536
Larry Hastings2f936352014-08-05 14:04:04 +10007537static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007538os_setuid_impl(PyObject *module, uid_t uid)
7539/*[clinic end generated code: output=a0a41fd0d1ec555f input=c921a3285aa22256]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007540{
Victor Stinner8c62be82010-05-06 00:08:46 +00007541 if (setuid(uid) < 0)
7542 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007543 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007544}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007545#endif /* HAVE_SETUID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007546
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007547
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007548#ifdef HAVE_SETEUID
Larry Hastings2f936352014-08-05 14:04:04 +10007549/*[clinic input]
7550os.seteuid
7551
7552 euid: uid_t
7553 /
7554
7555Set the current process's effective user id.
7556[clinic start generated code]*/
7557
Larry Hastings2f936352014-08-05 14:04:04 +10007558static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007559os_seteuid_impl(PyObject *module, uid_t euid)
7560/*[clinic end generated code: output=102e3ad98361519a input=ba93d927e4781aa9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007561{
7562 if (seteuid(euid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007563 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007564 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007565}
7566#endif /* HAVE_SETEUID */
7567
Larry Hastings2f936352014-08-05 14:04:04 +10007568
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007569#ifdef HAVE_SETEGID
Larry Hastings2f936352014-08-05 14:04:04 +10007570/*[clinic input]
7571os.setegid
7572
7573 egid: gid_t
7574 /
7575
7576Set the current process's effective group id.
7577[clinic start generated code]*/
7578
Larry Hastings2f936352014-08-05 14:04:04 +10007579static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007580os_setegid_impl(PyObject *module, gid_t egid)
7581/*[clinic end generated code: output=4e4b825a6a10258d input=4080526d0ccd6ce3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007582{
7583 if (setegid(egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007584 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007585 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007586}
7587#endif /* HAVE_SETEGID */
7588
Larry Hastings2f936352014-08-05 14:04:04 +10007589
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007590#ifdef HAVE_SETREUID
Larry Hastings2f936352014-08-05 14:04:04 +10007591/*[clinic input]
7592os.setreuid
7593
7594 ruid: uid_t
7595 euid: uid_t
7596 /
7597
7598Set the current process's real and effective user ids.
7599[clinic start generated code]*/
7600
Larry Hastings2f936352014-08-05 14:04:04 +10007601static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007602os_setreuid_impl(PyObject *module, uid_t ruid, uid_t euid)
7603/*[clinic end generated code: output=62d991210006530a input=0ca8978de663880c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007604{
Victor Stinner8c62be82010-05-06 00:08:46 +00007605 if (setreuid(ruid, euid) < 0) {
7606 return posix_error();
7607 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007608 Py_RETURN_NONE;
Victor Stinner8c62be82010-05-06 00:08:46 +00007609 }
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007610}
7611#endif /* HAVE_SETREUID */
7612
Larry Hastings2f936352014-08-05 14:04:04 +10007613
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007614#ifdef HAVE_SETREGID
Larry Hastings2f936352014-08-05 14:04:04 +10007615/*[clinic input]
7616os.setregid
7617
7618 rgid: gid_t
7619 egid: gid_t
7620 /
7621
7622Set the current process's real and effective group ids.
7623[clinic start generated code]*/
7624
Larry Hastings2f936352014-08-05 14:04:04 +10007625static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007626os_setregid_impl(PyObject *module, gid_t rgid, gid_t egid)
7627/*[clinic end generated code: output=aa803835cf5342f3 input=c59499f72846db78]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007628{
7629 if (setregid(rgid, egid) < 0)
Victor Stinner8c62be82010-05-06 00:08:46 +00007630 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007631 Py_RETURN_NONE;
Andrew M. Kuchling8d2f2b22000-07-13 01:26:58 +00007632}
7633#endif /* HAVE_SETREGID */
7634
Larry Hastings2f936352014-08-05 14:04:04 +10007635
Guido van Rossumb6775db1994-08-01 11:34:53 +00007636#ifdef HAVE_SETGID
Larry Hastings2f936352014-08-05 14:04:04 +10007637/*[clinic input]
7638os.setgid
7639 gid: gid_t
7640 /
7641
7642Set the current process's group id.
7643[clinic start generated code]*/
7644
Larry Hastings2f936352014-08-05 14:04:04 +10007645static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007646os_setgid_impl(PyObject *module, gid_t gid)
7647/*[clinic end generated code: output=bdccd7403f6ad8c3 input=27d30c4059045dc6]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007648{
Victor Stinner8c62be82010-05-06 00:08:46 +00007649 if (setgid(gid) < 0)
7650 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10007651 Py_RETURN_NONE;
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007652}
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00007653#endif /* HAVE_SETGID */
Guido van Rossuma3d78fb1993-11-10 09:23:53 +00007654
Larry Hastings2f936352014-08-05 14:04:04 +10007655
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007656#ifdef HAVE_SETGROUPS
Larry Hastings2f936352014-08-05 14:04:04 +10007657/*[clinic input]
7658os.setgroups
7659
7660 groups: object
7661 /
7662
7663Set the groups of the current process to list.
7664[clinic start generated code]*/
7665
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007666static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007667os_setgroups(PyObject *module, PyObject *groups)
7668/*[clinic end generated code: output=3fcb32aad58c5ecd input=fa742ca3daf85a7e]*/
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007669{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007670 Py_ssize_t i, len;
Victor Stinner8c62be82010-05-06 00:08:46 +00007671 gid_t grouplist[MAX_GROUPS];
Tim Peters5aa91602002-01-30 05:46:57 +00007672
Victor Stinner8c62be82010-05-06 00:08:46 +00007673 if (!PySequence_Check(groups)) {
7674 PyErr_SetString(PyExc_TypeError, "setgroups argument must be a sequence");
7675 return NULL;
7676 }
7677 len = PySequence_Size(groups);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03007678 if (len < 0) {
7679 return NULL;
7680 }
Victor Stinner8c62be82010-05-06 00:08:46 +00007681 if (len > MAX_GROUPS) {
7682 PyErr_SetString(PyExc_ValueError, "too many groups");
7683 return NULL;
7684 }
7685 for(i = 0; i < len; i++) {
7686 PyObject *elem;
7687 elem = PySequence_GetItem(groups, i);
7688 if (!elem)
7689 return NULL;
7690 if (!PyLong_Check(elem)) {
7691 PyErr_SetString(PyExc_TypeError,
7692 "groups must be integers");
7693 Py_DECREF(elem);
7694 return NULL;
7695 } else {
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007696 if (!_Py_Gid_Converter(elem, &grouplist[i])) {
Victor Stinner8c62be82010-05-06 00:08:46 +00007697 Py_DECREF(elem);
7698 return NULL;
7699 }
7700 }
7701 Py_DECREF(elem);
7702 }
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007703
Victor Stinner8c62be82010-05-06 00:08:46 +00007704 if (setgroups(len, grouplist) < 0)
7705 return posix_error();
Serhiy Storchaka228b12e2017-01-23 09:47:21 +02007706 Py_RETURN_NONE;
Martin v. Löwis61c5edf2001-10-18 04:06:00 +00007707}
7708#endif /* HAVE_SETGROUPS */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007709
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007710#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
7711static PyObject *
Victor Stinner4195b5c2012-02-08 23:03:19 +01007712wait_helper(pid_t pid, int status, struct rusage *ru)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007713{
Victor Stinner8c62be82010-05-06 00:08:46 +00007714 PyObject *result;
Eddie Elizondob3966632019-11-05 07:16:14 -08007715 PyObject *struct_rusage;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007716
Victor Stinner8c62be82010-05-06 00:08:46 +00007717 if (pid == -1)
7718 return posix_error();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007719
Zackery Spytz682107c2019-09-09 09:48:32 -06007720 // If wait succeeded but no child was ready to report status, ru will not
7721 // have been populated.
7722 if (pid == 0) {
7723 memset(ru, 0, sizeof(*ru));
7724 }
7725
Eddie Elizondob3966632019-11-05 07:16:14 -08007726 PyObject *m = PyImport_ImportModuleNoBlock("resource");
7727 if (m == NULL)
7728 return NULL;
7729 struct_rusage = PyObject_GetAttr(m, _posixstate_global->struct_rusage);
7730 Py_DECREF(m);
7731 if (struct_rusage == NULL)
7732 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007733
Victor Stinner8c62be82010-05-06 00:08:46 +00007734 /* XXX(nnorwitz): Copied (w/mods) from resource.c, there should be only one. */
7735 result = PyStructSequence_New((PyTypeObject*) struct_rusage);
Eddie Elizondoe4db1f02019-11-25 19:07:37 -08007736 Py_DECREF(struct_rusage);
Victor Stinner8c62be82010-05-06 00:08:46 +00007737 if (!result)
7738 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007739
7740#ifndef doubletime
7741#define doubletime(TV) ((double)(TV).tv_sec + (TV).tv_usec * 0.000001)
7742#endif
7743
Victor Stinner8c62be82010-05-06 00:08:46 +00007744 PyStructSequence_SET_ITEM(result, 0,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007745 PyFloat_FromDouble(doubletime(ru->ru_utime)));
Victor Stinner8c62be82010-05-06 00:08:46 +00007746 PyStructSequence_SET_ITEM(result, 1,
Victor Stinner4195b5c2012-02-08 23:03:19 +01007747 PyFloat_FromDouble(doubletime(ru->ru_stime)));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007748#define SET_INT(result, index, value)\
Victor Stinner8c62be82010-05-06 00:08:46 +00007749 PyStructSequence_SET_ITEM(result, index, PyLong_FromLong(value))
7750 SET_INT(result, 2, ru->ru_maxrss);
7751 SET_INT(result, 3, ru->ru_ixrss);
7752 SET_INT(result, 4, ru->ru_idrss);
7753 SET_INT(result, 5, ru->ru_isrss);
7754 SET_INT(result, 6, ru->ru_minflt);
7755 SET_INT(result, 7, ru->ru_majflt);
7756 SET_INT(result, 8, ru->ru_nswap);
7757 SET_INT(result, 9, ru->ru_inblock);
7758 SET_INT(result, 10, ru->ru_oublock);
7759 SET_INT(result, 11, ru->ru_msgsnd);
7760 SET_INT(result, 12, ru->ru_msgrcv);
7761 SET_INT(result, 13, ru->ru_nsignals);
7762 SET_INT(result, 14, ru->ru_nvcsw);
7763 SET_INT(result, 15, ru->ru_nivcsw);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007764#undef SET_INT
7765
Victor Stinner8c62be82010-05-06 00:08:46 +00007766 if (PyErr_Occurred()) {
7767 Py_DECREF(result);
7768 return NULL;
7769 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007770
Victor Stinner8c62be82010-05-06 00:08:46 +00007771 return Py_BuildValue("NiN", PyLong_FromPid(pid), status, result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007772}
7773#endif /* HAVE_WAIT3 || HAVE_WAIT4 */
7774
Larry Hastings2f936352014-08-05 14:04:04 +10007775
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007776#ifdef HAVE_WAIT3
Larry Hastings2f936352014-08-05 14:04:04 +10007777/*[clinic input]
7778os.wait3
7779
7780 options: int
7781Wait for completion of a child process.
7782
7783Returns a tuple of information about the child process:
7784 (pid, status, rusage)
7785[clinic start generated code]*/
7786
Larry Hastings2f936352014-08-05 14:04:04 +10007787static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007788os_wait3_impl(PyObject *module, int options)
7789/*[clinic end generated code: output=92c3224e6f28217a input=8ac4c56956b61710]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007790{
Victor Stinner8c62be82010-05-06 00:08:46 +00007791 pid_t pid;
Victor Stinner8c62be82010-05-06 00:08:46 +00007792 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007793 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007794 WAIT_TYPE status;
7795 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007796
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007797 do {
7798 Py_BEGIN_ALLOW_THREADS
7799 pid = wait3(&status, options, &ru);
7800 Py_END_ALLOW_THREADS
7801 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7802 if (pid < 0)
7803 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007804
Victor Stinner4195b5c2012-02-08 23:03:19 +01007805 return wait_helper(pid, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007806}
7807#endif /* HAVE_WAIT3 */
7808
Larry Hastings2f936352014-08-05 14:04:04 +10007809
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007810#ifdef HAVE_WAIT4
Larry Hastings2f936352014-08-05 14:04:04 +10007811/*[clinic input]
7812
7813os.wait4
7814
7815 pid: pid_t
7816 options: int
7817
7818Wait for completion of a specific child process.
7819
7820Returns a tuple of information about the child process:
7821 (pid, status, rusage)
7822[clinic start generated code]*/
7823
Larry Hastings2f936352014-08-05 14:04:04 +10007824static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007825os_wait4_impl(PyObject *module, pid_t pid, int options)
7826/*[clinic end generated code: output=66195aa507b35f70 input=d11deed0750600ba]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007827{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007828 pid_t res;
Victor Stinner8c62be82010-05-06 00:08:46 +00007829 struct rusage ru;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007830 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007831 WAIT_TYPE status;
7832 WAIT_STATUS_INT(status) = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007833
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007834 do {
7835 Py_BEGIN_ALLOW_THREADS
7836 res = wait4(pid, &status, options, &ru);
7837 Py_END_ALLOW_THREADS
7838 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7839 if (res < 0)
7840 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007841
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007842 return wait_helper(res, WAIT_STATUS_INT(status), &ru);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007843}
7844#endif /* HAVE_WAIT4 */
7845
Larry Hastings2f936352014-08-05 14:04:04 +10007846
Ross Lagerwall7807c352011-03-17 20:20:30 +02007847#if defined(HAVE_WAITID) && !defined(__APPLE__)
Larry Hastings2f936352014-08-05 14:04:04 +10007848/*[clinic input]
7849os.waitid
7850
7851 idtype: idtype_t
7852 Must be one of be P_PID, P_PGID or P_ALL.
7853 id: id_t
7854 The id to wait on.
7855 options: int
7856 Constructed from the ORing of one or more of WEXITED, WSTOPPED
7857 or WCONTINUED and additionally may be ORed with WNOHANG or WNOWAIT.
7858 /
7859
7860Returns the result of waiting for a process or processes.
7861
7862Returns either waitid_result or None if WNOHANG is specified and there are
7863no children in a waitable state.
7864[clinic start generated code]*/
7865
Larry Hastings2f936352014-08-05 14:04:04 +10007866static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007867os_waitid_impl(PyObject *module, idtype_t idtype, id_t id, int options)
7868/*[clinic end generated code: output=5d2e1c0bde61f4d8 input=d8e7f76e052b7920]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007869{
7870 PyObject *result;
7871 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007872 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007873 siginfo_t si;
7874 si.si_pid = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007875
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007876 do {
7877 Py_BEGIN_ALLOW_THREADS
7878 res = waitid(idtype, id, &si, options);
7879 Py_END_ALLOW_THREADS
7880 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7881 if (res < 0)
7882 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02007883
7884 if (si.si_pid == 0)
7885 Py_RETURN_NONE;
7886
Hai Shif707d942020-03-16 21:15:01 +08007887 PyObject *WaitidResultType = get_posix_state(module)->WaitidResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -08007888 result = PyStructSequence_New((PyTypeObject *)WaitidResultType);
Ross Lagerwall7807c352011-03-17 20:20:30 +02007889 if (!result)
7890 return NULL;
7891
7892 PyStructSequence_SET_ITEM(result, 0, PyLong_FromPid(si.si_pid));
Serhiy Storchaka7cf55992013-02-10 21:56:49 +02007893 PyStructSequence_SET_ITEM(result, 1, _PyLong_FromUid(si.si_uid));
Ross Lagerwall7807c352011-03-17 20:20:30 +02007894 PyStructSequence_SET_ITEM(result, 2, PyLong_FromLong((long)(si.si_signo)));
7895 PyStructSequence_SET_ITEM(result, 3, PyLong_FromLong((long)(si.si_status)));
7896 PyStructSequence_SET_ITEM(result, 4, PyLong_FromLong((long)(si.si_code)));
7897 if (PyErr_Occurred()) {
7898 Py_DECREF(result);
7899 return NULL;
7900 }
7901
7902 return result;
7903}
Larry Hastings2f936352014-08-05 14:04:04 +10007904#endif /* defined(HAVE_WAITID) && !defined(__APPLE__) */
Ross Lagerwall7807c352011-03-17 20:20:30 +02007905
Larry Hastings2f936352014-08-05 14:04:04 +10007906
7907#if defined(HAVE_WAITPID)
7908/*[clinic input]
7909os.waitpid
7910 pid: pid_t
7911 options: int
7912 /
7913
7914Wait for completion of a given child process.
7915
7916Returns a tuple of information regarding the child process:
7917 (pid, status)
7918
7919The options argument is ignored on Windows.
7920[clinic start generated code]*/
7921
Larry Hastings2f936352014-08-05 14:04:04 +10007922static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007923os_waitpid_impl(PyObject *module, pid_t pid, int options)
7924/*[clinic end generated code: output=5c37c06887a20270 input=0bf1666b8758fda3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007925{
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007926 pid_t res;
7927 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007928 WAIT_TYPE status;
7929 WAIT_STATUS_INT(status) = 0;
Guido van Rossum54ecc3d1999-01-27 17:53:11 +00007930
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007931 do {
7932 Py_BEGIN_ALLOW_THREADS
7933 res = waitpid(pid, &status, options);
7934 Py_END_ALLOW_THREADS
7935 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
7936 if (res < 0)
7937 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007938
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007939 return Py_BuildValue("Ni", PyLong_FromPid(res), WAIT_STATUS_INT(status));
Guido van Rossum21803b81992-08-09 12:55:27 +00007940}
Tim Petersab034fa2002-02-01 11:27:43 +00007941#elif defined(HAVE_CWAIT)
Tim Petersab034fa2002-02-01 11:27:43 +00007942/* MS C has a variant of waitpid() that's usable for most purposes. */
Larry Hastings2f936352014-08-05 14:04:04 +10007943/*[clinic input]
7944os.waitpid
Benjamin Petersonca470632016-09-06 13:47:26 -07007945 pid: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +10007946 options: int
7947 /
7948
7949Wait for completion of a given process.
7950
7951Returns a tuple of information regarding the process:
7952 (pid, status << 8)
7953
7954The options argument is ignored on Windows.
7955[clinic start generated code]*/
7956
Larry Hastings2f936352014-08-05 14:04:04 +10007957static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -07007958os_waitpid_impl(PyObject *module, intptr_t pid, int options)
Victor Stinner581139c2016-09-06 15:54:20 -07007959/*[clinic end generated code: output=be836b221271d538 input=40f2440c515410f8]*/
Larry Hastings2f936352014-08-05 14:04:04 +10007960{
7961 int status;
Benjamin Petersonca470632016-09-06 13:47:26 -07007962 intptr_t res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007963 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10007964
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007965 do {
7966 Py_BEGIN_ALLOW_THREADS
Steve Dower11f43262016-11-19 18:33:39 -08007967 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007968 res = _cwait(&status, pid, options);
Steve Dower11f43262016-11-19 18:33:39 -08007969 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007970 Py_END_ALLOW_THREADS
7971 } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinnerd3ffd322015-09-15 10:11:03 +02007972 if (res < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007973 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007974
Victor Stinner9bee32b2020-04-22 16:30:35 +02007975 unsigned long long ustatus = (unsigned int)status;
7976
Victor Stinner8c62be82010-05-06 00:08:46 +00007977 /* shift the status left a byte so this is more like the POSIX waitpid */
Victor Stinner9bee32b2020-04-22 16:30:35 +02007978 return Py_BuildValue(_Py_PARSE_INTPTR "K", res, ustatus << 8);
Tim Petersab034fa2002-02-01 11:27:43 +00007979}
Larry Hastings2f936352014-08-05 14:04:04 +10007980#endif
7981
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00007982
Guido van Rossumad0ee831995-03-01 10:34:45 +00007983#ifdef HAVE_WAIT
Larry Hastings2f936352014-08-05 14:04:04 +10007984/*[clinic input]
7985os.wait
7986
7987Wait for completion of a child process.
7988
7989Returns a tuple of information about the child process:
7990 (pid, status)
7991[clinic start generated code]*/
7992
Larry Hastings2f936352014-08-05 14:04:04 +10007993static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03007994os_wait_impl(PyObject *module)
7995/*[clinic end generated code: output=6bc419ac32fb364b input=03b0182d4a4700ce]*/
Guido van Rossum21803b81992-08-09 12:55:27 +00007996{
Victor Stinner8c62be82010-05-06 00:08:46 +00007997 pid_t pid;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00007998 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +00007999 WAIT_TYPE status;
8000 WAIT_STATUS_INT(status) = 0;
Neal Norwitze241ce82003-02-17 18:17:05 +00008001
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008002 do {
8003 Py_BEGIN_ALLOW_THREADS
8004 pid = wait(&status);
8005 Py_END_ALLOW_THREADS
8006 } while (pid < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
8007 if (pid < 0)
8008 return (!async_err) ? posix_error() : NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00008009
Victor Stinner8c62be82010-05-06 00:08:46 +00008010 return Py_BuildValue("Ni", PyLong_FromPid(pid), WAIT_STATUS_INT(status));
Guido van Rossum85e3b011991-06-03 12:42:10 +00008011}
Larry Hastings2f936352014-08-05 14:04:04 +10008012#endif /* HAVE_WAIT */
Guido van Rossum85e3b011991-06-03 12:42:10 +00008013
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -08008014#if defined(__linux__) && defined(__NR_pidfd_open)
8015/*[clinic input]
8016os.pidfd_open
8017 pid: pid_t
8018 flags: unsigned_int = 0
8019
8020Return a file descriptor referring to the process *pid*.
8021
8022The descriptor can be used to perform process management without races and
8023signals.
8024[clinic start generated code]*/
8025
8026static PyObject *
8027os_pidfd_open_impl(PyObject *module, pid_t pid, unsigned int flags)
8028/*[clinic end generated code: output=5c7252698947dc41 input=c3fd99ce947ccfef]*/
8029{
8030 int fd = syscall(__NR_pidfd_open, pid, flags);
8031 if (fd < 0) {
8032 return posix_error();
8033 }
8034 return PyLong_FromLong(fd);
8035}
8036#endif
8037
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008038
Larry Hastings9cf065c2012-06-22 16:30:09 -07008039#if defined(HAVE_READLINK) || defined(MS_WINDOWS)
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008040/*[clinic input]
8041os.readlink
8042
8043 path: path_t
8044 *
8045 dir_fd: dir_fd(requires='readlinkat') = None
8046
8047Return a string representing the path to which the symbolic link points.
8048
8049If dir_fd is not None, it should be a file descriptor open to a directory,
8050and path should be relative; path will then be relative to that directory.
8051
8052dir_fd may not be implemented on your platform. If it is unavailable,
8053using it will raise a NotImplementedError.
8054[clinic start generated code]*/
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008055
Barry Warsaw53699e91996-12-10 23:23:01 +00008056static PyObject *
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008057os_readlink_impl(PyObject *module, path_t *path, int dir_fd)
8058/*[clinic end generated code: output=d21b732a2e814030 input=113c87e0db1ecaf2]*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00008059{
Berker Peksage0b5b202018-08-15 13:03:41 +03008060#if defined(HAVE_READLINK)
Christian Heimes3cb091e2016-09-23 20:24:28 +02008061 char buffer[MAXPATHLEN+1];
Larry Hastings9cf065c2012-06-22 16:30:09 -07008062 ssize_t length;
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008063
8064 Py_BEGIN_ALLOW_THREADS
8065#ifdef HAVE_READLINKAT
8066 if (dir_fd != DEFAULT_DIR_FD)
8067 length = readlinkat(dir_fd, path->narrow, buffer, MAXPATHLEN);
8068 else
8069#endif
8070 length = readlink(path->narrow, buffer, MAXPATHLEN);
8071 Py_END_ALLOW_THREADS
8072
8073 if (length < 0) {
8074 return path_error(path);
8075 }
8076 buffer[length] = '\0';
8077
8078 if (PyUnicode_Check(path->object))
8079 return PyUnicode_DecodeFSDefaultAndSize(buffer, length);
8080 else
8081 return PyBytes_FromStringAndSize(buffer, length);
Berker Peksage0b5b202018-08-15 13:03:41 +03008082#elif defined(MS_WINDOWS)
8083 DWORD n_bytes_returned;
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008084 DWORD io_result = 0;
Berker Peksage0b5b202018-08-15 13:03:41 +03008085 HANDLE reparse_point_handle;
Berker Peksage0b5b202018-08-15 13:03:41 +03008086 char target_buffer[_Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE];
8087 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
Steve Dower993ac922019-09-03 12:50:51 -07008088 PyObject *result = NULL;
Thomas Wouters89f507f2006-12-13 04:49:30 +00008089
Larry Hastings2f936352014-08-05 14:04:04 +10008090 /* First get a handle to the reparse point */
8091 Py_BEGIN_ALLOW_THREADS
8092 reparse_point_handle = CreateFileW(
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008093 path->wide,
Larry Hastings2f936352014-08-05 14:04:04 +10008094 0,
8095 0,
8096 0,
8097 OPEN_EXISTING,
8098 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
8099 0);
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008100 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
8101 /* New call DeviceIoControl to read the reparse point */
8102 io_result = DeviceIoControl(
8103 reparse_point_handle,
8104 FSCTL_GET_REPARSE_POINT,
8105 0, 0, /* in buffer */
8106 target_buffer, sizeof(target_buffer),
8107 &n_bytes_returned,
8108 0 /* we're not using OVERLAPPED_IO */
8109 );
8110 CloseHandle(reparse_point_handle);
Berker Peksage0b5b202018-08-15 13:03:41 +03008111 }
Larry Hastings2f936352014-08-05 14:04:04 +10008112 Py_END_ALLOW_THREADS
8113
Berker Peksage0b5b202018-08-15 13:03:41 +03008114 if (io_result == 0) {
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008115 return path_error(path);
Berker Peksage0b5b202018-08-15 13:03:41 +03008116 }
Larry Hastings2f936352014-08-05 14:04:04 +10008117
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008118 wchar_t *name = NULL;
8119 Py_ssize_t nameLen = 0;
8120 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
Larry Hastings2f936352014-08-05 14:04:04 +10008121 {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008122 name = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
8123 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
8124 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
Larry Hastings2f936352014-08-05 14:04:04 +10008125 }
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008126 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
8127 {
8128 name = (wchar_t *)((char*)rdb->MountPointReparseBuffer.PathBuffer +
8129 rdb->MountPointReparseBuffer.SubstituteNameOffset);
8130 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
8131 }
8132 else
8133 {
8134 PyErr_SetString(PyExc_ValueError, "not a symbolic link");
8135 }
8136 if (name) {
8137 if (nameLen > 4 && wcsncmp(name, L"\\??\\", 4) == 0) {
8138 /* Our buffer is mutable, so this is okay */
8139 name[1] = L'\\';
8140 }
8141 result = PyUnicode_FromWideChar(name, nameLen);
Steve Dower993ac922019-09-03 12:50:51 -07008142 if (result && path->narrow) {
Steve Dowerdf2d4a62019-08-21 15:27:33 -07008143 Py_SETREF(result, PyUnicode_EncodeFSDefault(result));
8144 }
Berker Peksage0b5b202018-08-15 13:03:41 +03008145 }
Serhiy Storchaka12a69db2018-09-17 15:38:27 +03008146 return result;
Berker Peksage0b5b202018-08-15 13:03:41 +03008147#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008148}
Berker Peksage0b5b202018-08-15 13:03:41 +03008149#endif /* defined(HAVE_READLINK) || defined(MS_WINDOWS) */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008150
Larry Hastings9cf065c2012-06-22 16:30:09 -07008151#ifdef HAVE_SYMLINK
Larry Hastings9cf065c2012-06-22 16:30:09 -07008152
8153#if defined(MS_WINDOWS)
8154
Steve Dower6921e732018-03-05 14:26:08 -08008155/* Remove the last portion of the path - return 0 on success */
8156static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008157_dirnameW(WCHAR *path)
8158{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008159 WCHAR *ptr;
Steve Dower6921e732018-03-05 14:26:08 -08008160 size_t length = wcsnlen_s(path, MAX_PATH);
8161 if (length == MAX_PATH) {
8162 return -1;
8163 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008164
8165 /* walk the path from the end until a backslash is encountered */
Steve Dower6921e732018-03-05 14:26:08 -08008166 for(ptr = path + length; ptr != path; ptr--) {
8167 if (*ptr == L'\\' || *ptr == L'/') {
Jason R. Coombs3a092862013-05-27 23:21:28 -04008168 break;
Steve Dower6921e732018-03-05 14:26:08 -08008169 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008170 }
8171 *ptr = 0;
Steve Dower6921e732018-03-05 14:26:08 -08008172 return 0;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008173}
8174
Victor Stinner31b3b922013-06-05 01:49:17 +02008175/* Is this path absolute? */
8176static int
8177_is_absW(const WCHAR *path)
8178{
Steve Dower6921e732018-03-05 14:26:08 -08008179 return path[0] == L'\\' || path[0] == L'/' ||
8180 (path[0] && path[1] == L':');
Jason R. Coombs3a092862013-05-27 23:21:28 -04008181}
8182
Steve Dower6921e732018-03-05 14:26:08 -08008183/* join root and rest with a backslash - return 0 on success */
8184static int
Victor Stinner31b3b922013-06-05 01:49:17 +02008185_joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
8186{
Victor Stinner31b3b922013-06-05 01:49:17 +02008187 if (_is_absW(rest)) {
Steve Dower6921e732018-03-05 14:26:08 -08008188 return wcscpy_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008189 }
8190
Steve Dower6921e732018-03-05 14:26:08 -08008191 if (wcscpy_s(dest_path, MAX_PATH, root)) {
8192 return -1;
Jason R. Coombs3a092862013-05-27 23:21:28 -04008193 }
Steve Dower6921e732018-03-05 14:26:08 -08008194
8195 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
8196 return -1;
8197 }
8198
8199 return wcscat_s(dest_path, MAX_PATH, rest);
Jason R. Coombs3a092862013-05-27 23:21:28 -04008200}
8201
Victor Stinner31b3b922013-06-05 01:49:17 +02008202/* Return True if the path at src relative to dest is a directory */
8203static int
Serhiy Storchakadeab18d2016-05-07 16:45:18 +03008204_check_dirW(LPCWSTR src, LPCWSTR dest)
Jason R. Coombs3a092862013-05-27 23:21:28 -04008205{
Jason R. Coombs3a092862013-05-27 23:21:28 -04008206 WIN32_FILE_ATTRIBUTE_DATA src_info;
8207 WCHAR dest_parent[MAX_PATH];
8208 WCHAR src_resolved[MAX_PATH] = L"";
8209
8210 /* dest_parent = os.path.dirname(dest) */
Steve Dower6921e732018-03-05 14:26:08 -08008211 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
8212 _dirnameW(dest_parent)) {
8213 return 0;
8214 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008215 /* src_resolved = os.path.join(dest_parent, src) */
Steve Dower6921e732018-03-05 14:26:08 -08008216 if (_joinW(src_resolved, dest_parent, src)) {
8217 return 0;
8218 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008219 return (
8220 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
8221 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
8222 );
8223}
Larry Hastings9cf065c2012-06-22 16:30:09 -07008224#endif
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008225
Larry Hastings2f936352014-08-05 14:04:04 +10008226
8227/*[clinic input]
8228os.symlink
8229 src: path_t
8230 dst: path_t
8231 target_is_directory: bool = False
8232 *
8233 dir_fd: dir_fd(requires='symlinkat')=None
8234
8235# "symlink(src, dst, target_is_directory=False, *, dir_fd=None)\n\n\
8236
8237Create a symbolic link pointing to src named dst.
8238
8239target_is_directory is required on Windows if the target is to be
8240 interpreted as a directory. (On Windows, symlink requires
8241 Windows 6.0 or greater, and raises a NotImplementedError otherwise.)
8242 target_is_directory is ignored on non-Windows platforms.
8243
8244If dir_fd is not None, it should be a file descriptor open to a directory,
8245 and path should be relative; path will then be relative to that directory.
8246dir_fd may not be implemented on your platform.
8247 If it is unavailable, using it will raise a NotImplementedError.
8248
8249[clinic start generated code]*/
8250
Larry Hastings2f936352014-08-05 14:04:04 +10008251static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008252os_symlink_impl(PyObject *module, path_t *src, path_t *dst,
Larry Hastings89964c42015-04-14 18:07:59 -04008253 int target_is_directory, int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008254/*[clinic end generated code: output=08ca9f3f3cf960f6 input=e820ec4472547bc3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008255{
Larry Hastings9cf065c2012-06-22 16:30:09 -07008256#ifdef MS_WINDOWS
8257 DWORD result;
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008258 DWORD flags = 0;
8259
8260 /* Assumed true, set to false if detected to not be available. */
8261 static int windows_has_symlink_unprivileged_flag = TRUE;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008262#else
8263 int result;
8264#endif
8265
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008266 if (PySys_Audit("os.symlink", "OOi", src->object, dst->object,
8267 dir_fd == DEFAULT_DIR_FD ? -1 : dir_fd) < 0) {
8268 return NULL;
8269 }
8270
Larry Hastings9cf065c2012-06-22 16:30:09 -07008271#ifdef MS_WINDOWS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008272
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008273 if (windows_has_symlink_unprivileged_flag) {
8274 /* Allow non-admin symlinks if system allows it. */
8275 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
8276 }
Jason R. Coombs3a092862013-05-27 23:21:28 -04008277
Larry Hastings9cf065c2012-06-22 16:30:09 -07008278 Py_BEGIN_ALLOW_THREADS
Steve Dower6921e732018-03-05 14:26:08 -08008279 _Py_BEGIN_SUPPRESS_IPH
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008280 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
8281 if (target_is_directory || _check_dirW(src->wide, dst->wide)) {
8282 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
8283 }
8284
8285 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
Steve Dower6921e732018-03-05 14:26:08 -08008286 _Py_END_SUPPRESS_IPH
Larry Hastings9cf065c2012-06-22 16:30:09 -07008287 Py_END_ALLOW_THREADS
8288
Vidar Tonaas Fauske0e107662019-04-09 20:19:46 +02008289 if (windows_has_symlink_unprivileged_flag && !result &&
8290 ERROR_INVALID_PARAMETER == GetLastError()) {
8291
8292 Py_BEGIN_ALLOW_THREADS
8293 _Py_BEGIN_SUPPRESS_IPH
8294 /* This error might be caused by
8295 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
8296 Try again, and update windows_has_symlink_unprivileged_flag if we
8297 are successful this time.
8298
8299 NOTE: There is a risk of a race condition here if there are other
8300 conditions than the flag causing ERROR_INVALID_PARAMETER, and
8301 another process (or thread) changes that condition in between our
8302 calls to CreateSymbolicLink.
8303 */
8304 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
8305 result = CreateSymbolicLinkW(dst->wide, src->wide, flags);
8306 _Py_END_SUPPRESS_IPH
8307 Py_END_ALLOW_THREADS
8308
8309 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
8310 windows_has_symlink_unprivileged_flag = FALSE;
8311 }
8312 }
8313
Larry Hastings2f936352014-08-05 14:04:04 +10008314 if (!result)
8315 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008316
8317#else
8318
Steve Dower6921e732018-03-05 14:26:08 -08008319 if ((src->narrow && dst->wide) || (src->wide && dst->narrow)) {
8320 PyErr_SetString(PyExc_ValueError,
8321 "symlink: src and dst must be the same type");
8322 return NULL;
8323 }
8324
Larry Hastings9cf065c2012-06-22 16:30:09 -07008325 Py_BEGIN_ALLOW_THREADS
8326#if HAVE_SYMLINKAT
8327 if (dir_fd != DEFAULT_DIR_FD)
Larry Hastings2f936352014-08-05 14:04:04 +10008328 result = symlinkat(src->narrow, dir_fd, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008329 else
8330#endif
Larry Hastings2f936352014-08-05 14:04:04 +10008331 result = symlink(src->narrow, dst->narrow);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008332 Py_END_ALLOW_THREADS
8333
Larry Hastings2f936352014-08-05 14:04:04 +10008334 if (result)
8335 return path_error2(src, dst);
Larry Hastings9cf065c2012-06-22 16:30:09 -07008336#endif
8337
Larry Hastings2f936352014-08-05 14:04:04 +10008338 Py_RETURN_NONE;
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008339}
8340#endif /* HAVE_SYMLINK */
8341
Larry Hastings9cf065c2012-06-22 16:30:09 -07008342
Brian Curtind40e6f72010-07-08 21:39:08 +00008343
Guido van Rossumbfaf3d61997-12-29 20:02:27 +00008344
Larry Hastings605a62d2012-06-24 04:33:36 -07008345static PyStructSequence_Field times_result_fields[] = {
8346 {"user", "user time"},
8347 {"system", "system time"},
8348 {"children_user", "user time of children"},
8349 {"children_system", "system time of children"},
8350 {"elapsed", "elapsed time since an arbitrary point in the past"},
8351 {NULL}
8352};
8353
8354PyDoc_STRVAR(times_result__doc__,
8355"times_result: Result from os.times().\n\n\
8356This object may be accessed either as a tuple of\n\
8357 (user, system, children_user, children_system, elapsed),\n\
8358or via the attributes user, system, children_user, children_system,\n\
8359and elapsed.\n\
8360\n\
8361See os.times for more information.");
8362
8363static PyStructSequence_Desc times_result_desc = {
8364 "times_result", /* name */
8365 times_result__doc__, /* doc */
8366 times_result_fields,
8367 5
8368};
8369
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008370#ifdef MS_WINDOWS
8371#define HAVE_TIMES /* mandatory, for the method table */
8372#endif
Larry Hastings605a62d2012-06-24 04:33:36 -07008373
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008374#ifdef HAVE_TIMES
Larry Hastings605a62d2012-06-24 04:33:36 -07008375
8376static PyObject *
8377build_times_result(double user, double system,
8378 double children_user, double children_system,
8379 double elapsed)
8380{
Eddie Elizondob3966632019-11-05 07:16:14 -08008381 PyObject *TimesResultType = _posixstate_global->TimesResultType;
8382 PyObject *value = PyStructSequence_New((PyTypeObject *)TimesResultType);
Larry Hastings605a62d2012-06-24 04:33:36 -07008383 if (value == NULL)
8384 return NULL;
8385
8386#define SET(i, field) \
8387 { \
8388 PyObject *o = PyFloat_FromDouble(field); \
8389 if (!o) { \
8390 Py_DECREF(value); \
8391 return NULL; \
8392 } \
8393 PyStructSequence_SET_ITEM(value, i, o); \
8394 } \
8395
8396 SET(0, user);
8397 SET(1, system);
8398 SET(2, children_user);
8399 SET(3, children_system);
8400 SET(4, elapsed);
8401
8402#undef SET
8403
8404 return value;
8405}
8406
Larry Hastings605a62d2012-06-24 04:33:36 -07008407
Larry Hastings2f936352014-08-05 14:04:04 +10008408#ifndef MS_WINDOWS
8409#define NEED_TICKS_PER_SECOND
8410static long ticks_per_second = -1;
8411#endif /* MS_WINDOWS */
8412
8413/*[clinic input]
8414os.times
8415
8416Return a collection containing process timing information.
8417
8418The object returned behaves like a named tuple with these fields:
8419 (utime, stime, cutime, cstime, elapsed_time)
8420All fields are floating point numbers.
8421[clinic start generated code]*/
8422
Larry Hastings2f936352014-08-05 14:04:04 +10008423static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008424os_times_impl(PyObject *module)
8425/*[clinic end generated code: output=35f640503557d32a input=2bf9df3d6ab2e48b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008426#ifdef MS_WINDOWS
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008427{
Victor Stinner8c62be82010-05-06 00:08:46 +00008428 FILETIME create, exit, kernel, user;
8429 HANDLE hProc;
8430 hProc = GetCurrentProcess();
8431 GetProcessTimes(hProc, &create, &exit, &kernel, &user);
8432 /* The fields of a FILETIME structure are the hi and lo part
8433 of a 64-bit value expressed in 100 nanosecond units.
8434 1e7 is one second in such units; 1e-7 the inverse.
8435 429.4967296 is 2**32 / 1e7 or 2**32 * 1e-7.
8436 */
Larry Hastings605a62d2012-06-24 04:33:36 -07008437 return build_times_result(
Victor Stinner8c62be82010-05-06 00:08:46 +00008438 (double)(user.dwHighDateTime*429.4967296 +
8439 user.dwLowDateTime*1e-7),
8440 (double)(kernel.dwHighDateTime*429.4967296 +
8441 kernel.dwLowDateTime*1e-7),
8442 (double)0,
8443 (double)0,
8444 (double)0);
Guido van Rossum14ed0b21994-09-29 09:50:09 +00008445}
Larry Hastings2f936352014-08-05 14:04:04 +10008446#else /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008447{
Larry Hastings2f936352014-08-05 14:04:04 +10008448
8449
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008450 struct tms t;
8451 clock_t c;
8452 errno = 0;
8453 c = times(&t);
8454 if (c == (clock_t) -1)
8455 return posix_error();
8456 return build_times_result(
8457 (double)t.tms_utime / ticks_per_second,
8458 (double)t.tms_stime / ticks_per_second,
8459 (double)t.tms_cutime / ticks_per_second,
8460 (double)t.tms_cstime / ticks_per_second,
8461 (double)c / ticks_per_second);
8462}
Larry Hastings2f936352014-08-05 14:04:04 +10008463#endif /* MS_WINDOWS */
Antoine Pitrouf3923e92012-07-24 21:23:53 +02008464#endif /* HAVE_TIMES */
8465
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008466
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008467#ifdef HAVE_GETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008468/*[clinic input]
8469os.getsid
8470
8471 pid: pid_t
8472 /
8473
8474Call the system call getsid(pid) and return the result.
8475[clinic start generated code]*/
8476
Larry Hastings2f936352014-08-05 14:04:04 +10008477static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008478os_getsid_impl(PyObject *module, pid_t pid)
8479/*[clinic end generated code: output=112deae56b306460 input=eeb2b923a30ce04e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008480{
Victor Stinner8c62be82010-05-06 00:08:46 +00008481 int sid;
Victor Stinner8c62be82010-05-06 00:08:46 +00008482 sid = getsid(pid);
8483 if (sid < 0)
8484 return posix_error();
8485 return PyLong_FromLong((long)sid);
Martin v. Löwis49ee14d2003-11-10 06:35:36 +00008486}
8487#endif /* HAVE_GETSID */
8488
8489
Guido van Rossumb6775db1994-08-01 11:34:53 +00008490#ifdef HAVE_SETSID
Larry Hastings2f936352014-08-05 14:04:04 +10008491/*[clinic input]
8492os.setsid
8493
8494Call the system call setsid().
8495[clinic start generated code]*/
8496
Larry Hastings2f936352014-08-05 14:04:04 +10008497static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008498os_setsid_impl(PyObject *module)
8499/*[clinic end generated code: output=e2ddedd517086d77 input=5fff45858e2f0776]*/
Guido van Rossumc2670a01992-09-13 20:07:29 +00008500{
Victor Stinner8c62be82010-05-06 00:08:46 +00008501 if (setsid() < 0)
8502 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008503 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008504}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008505#endif /* HAVE_SETSID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008506
Larry Hastings2f936352014-08-05 14:04:04 +10008507
Guido van Rossumb6775db1994-08-01 11:34:53 +00008508#ifdef HAVE_SETPGID
Larry Hastings2f936352014-08-05 14:04:04 +10008509/*[clinic input]
8510os.setpgid
8511
8512 pid: pid_t
8513 pgrp: pid_t
8514 /
8515
8516Call the system call setpgid(pid, pgrp).
8517[clinic start generated code]*/
8518
Larry Hastings2f936352014-08-05 14:04:04 +10008519static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008520os_setpgid_impl(PyObject *module, pid_t pid, pid_t pgrp)
8521/*[clinic end generated code: output=6461160319a43d6a input=fceb395eca572e1a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008522{
Victor Stinner8c62be82010-05-06 00:08:46 +00008523 if (setpgid(pid, pgrp) < 0)
8524 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008525 Py_RETURN_NONE;
Guido van Rossumc2670a01992-09-13 20:07:29 +00008526}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008527#endif /* HAVE_SETPGID */
Guido van Rossumc2670a01992-09-13 20:07:29 +00008528
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008529
Guido van Rossumb6775db1994-08-01 11:34:53 +00008530#ifdef HAVE_TCGETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008531/*[clinic input]
8532os.tcgetpgrp
8533
8534 fd: int
8535 /
8536
8537Return the process group associated with the terminal specified by fd.
8538[clinic start generated code]*/
8539
Larry Hastings2f936352014-08-05 14:04:04 +10008540static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008541os_tcgetpgrp_impl(PyObject *module, int fd)
8542/*[clinic end generated code: output=f865e88be86c272b input=7f6c18eac10ada86]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008543{
8544 pid_t pgid = tcgetpgrp(fd);
Victor Stinner8c62be82010-05-06 00:08:46 +00008545 if (pgid < 0)
8546 return posix_error();
8547 return PyLong_FromPid(pgid);
Guido van Rossum7066dd71992-09-17 17:54:56 +00008548}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008549#endif /* HAVE_TCGETPGRP */
Guido van Rossum7066dd71992-09-17 17:54:56 +00008550
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008551
Guido van Rossumb6775db1994-08-01 11:34:53 +00008552#ifdef HAVE_TCSETPGRP
Larry Hastings2f936352014-08-05 14:04:04 +10008553/*[clinic input]
8554os.tcsetpgrp
8555
8556 fd: int
8557 pgid: pid_t
8558 /
8559
8560Set the process group associated with the terminal specified by fd.
8561[clinic start generated code]*/
8562
Larry Hastings2f936352014-08-05 14:04:04 +10008563static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008564os_tcsetpgrp_impl(PyObject *module, int fd, pid_t pgid)
8565/*[clinic end generated code: output=f1821a381b9daa39 input=5bdc997c6a619020]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008566{
Victor Stinner8c62be82010-05-06 00:08:46 +00008567 if (tcsetpgrp(fd, pgid) < 0)
8568 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008569 Py_RETURN_NONE;
Guido van Rossum7066dd71992-09-17 17:54:56 +00008570}
Guido van Rossumb6775db1994-08-01 11:34:53 +00008571#endif /* HAVE_TCSETPGRP */
Guido van Rossum22db57e1992-04-05 14:25:30 +00008572
Guido van Rossum687dd131993-05-17 08:34:16 +00008573/* Functions acting on file descriptors */
8574
Victor Stinnerdaf45552013-08-28 00:53:59 +02008575#ifdef O_CLOEXEC
8576extern int _Py_open_cloexec_works;
8577#endif
8578
Larry Hastings2f936352014-08-05 14:04:04 +10008579
8580/*[clinic input]
8581os.open -> int
8582 path: path_t
8583 flags: int
8584 mode: int = 0o777
8585 *
8586 dir_fd: dir_fd(requires='openat') = None
8587
8588# "open(path, flags, mode=0o777, *, dir_fd=None)\n\n\
8589
8590Open a file for low level IO. Returns a file descriptor (integer).
8591
8592If dir_fd is not None, it should be a file descriptor open to a directory,
8593 and path should be relative; path will then be relative to that directory.
8594dir_fd may not be implemented on your platform.
8595 If it is unavailable, using it will raise a NotImplementedError.
8596[clinic start generated code]*/
8597
Larry Hastings2f936352014-08-05 14:04:04 +10008598static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008599os_open_impl(PyObject *module, path_t *path, int flags, int mode, int dir_fd)
8600/*[clinic end generated code: output=abc7227888c8bc73 input=ad8623b29acd2934]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008601{
8602 int fd;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008603 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10008604
Victor Stinnerdaf45552013-08-28 00:53:59 +02008605#ifdef O_CLOEXEC
8606 int *atomic_flag_works = &_Py_open_cloexec_works;
8607#elif !defined(MS_WINDOWS)
8608 int *atomic_flag_works = NULL;
8609#endif
Mark Hammondc2e85bd2002-10-03 05:10:39 +00008610
Victor Stinnerdaf45552013-08-28 00:53:59 +02008611#ifdef MS_WINDOWS
8612 flags |= O_NOINHERIT;
8613#elif defined(O_CLOEXEC)
8614 flags |= O_CLOEXEC;
8615#endif
8616
Steve Dowerb82e17e2019-05-23 08:45:22 -07008617 if (PySys_Audit("open", "OOi", path->object, Py_None, flags) < 0) {
8618 return -1;
8619 }
8620
Steve Dower8fc89802015-04-12 00:26:27 -04008621 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008622 do {
8623 Py_BEGIN_ALLOW_THREADS
Larry Hastings9cf065c2012-06-22 16:30:09 -07008624#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -07008625 fd = _wopen(path->wide, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008626#else
Larry Hastings9cf065c2012-06-22 16:30:09 -07008627#ifdef HAVE_OPENAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008628 if (dir_fd != DEFAULT_DIR_FD)
8629 fd = openat(dir_fd, path->narrow, flags, mode);
8630 else
Steve Dower6230aaf2016-09-09 09:03:15 -07008631#endif /* HAVE_OPENAT */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008632 fd = open(path->narrow, flags, mode);
Steve Dower6230aaf2016-09-09 09:03:15 -07008633#endif /* !MS_WINDOWS */
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008634 Py_END_ALLOW_THREADS
8635 } while (fd < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04008636 _Py_END_SUPPRESS_IPH
Guido van Rossum687dd131993-05-17 08:34:16 +00008637
Victor Stinnerd3ffd322015-09-15 10:11:03 +02008638 if (fd < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008639 if (!async_err)
8640 PyErr_SetFromErrnoWithFilenameObject(PyExc_OSError, path->object);
Larry Hastings2f936352014-08-05 14:04:04 +10008641 return -1;
Larry Hastings9cf065c2012-06-22 16:30:09 -07008642 }
8643
Victor Stinnerdaf45552013-08-28 00:53:59 +02008644#ifndef MS_WINDOWS
8645 if (_Py_set_inheritable(fd, 0, atomic_flag_works) < 0) {
8646 close(fd);
Larry Hastings2f936352014-08-05 14:04:04 +10008647 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008648 }
8649#endif
8650
Larry Hastings2f936352014-08-05 14:04:04 +10008651 return fd;
8652}
8653
8654
8655/*[clinic input]
8656os.close
8657
8658 fd: int
8659
8660Close a file descriptor.
8661[clinic start generated code]*/
8662
Barry Warsaw53699e91996-12-10 23:23:01 +00008663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008664os_close_impl(PyObject *module, int fd)
8665/*[clinic end generated code: output=2fe4e93602822c14 input=2bc42451ca5c3223]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00008666{
Larry Hastings2f936352014-08-05 14:04:04 +10008667 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008668 /* We do not want to retry upon EINTR: see http://lwn.net/Articles/576478/
8669 * and http://linux.derkeiler.com/Mailing-Lists/Kernel/2005-09/3000.html
8670 * for more details.
8671 */
Victor Stinner8c62be82010-05-06 00:08:46 +00008672 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008673 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008674 res = close(fd);
Steve Dower8fc89802015-04-12 00:26:27 -04008675 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008676 Py_END_ALLOW_THREADS
8677 if (res < 0)
8678 return posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10008679 Py_RETURN_NONE;
Guido van Rossum687dd131993-05-17 08:34:16 +00008680}
8681
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008682
Jakub Kulíke20134f2019-09-11 17:11:57 +02008683#ifdef HAVE_FDWALK
8684static int
8685_fdwalk_close_func(void *lohi, int fd)
8686{
8687 int lo = ((int *)lohi)[0];
8688 int hi = ((int *)lohi)[1];
8689
Victor Stinner162c5672020-04-24 12:00:51 +02008690 if (fd >= hi) {
Jakub Kulíke20134f2019-09-11 17:11:57 +02008691 return 1;
Victor Stinner162c5672020-04-24 12:00:51 +02008692 }
8693 else if (fd >= lo) {
8694 /* Ignore errors */
8695 (void)close(fd);
8696 }
Jakub Kulíke20134f2019-09-11 17:11:57 +02008697 return 0;
8698}
8699#endif /* HAVE_FDWALK */
8700
Larry Hastings2f936352014-08-05 14:04:04 +10008701/*[clinic input]
8702os.closerange
8703
8704 fd_low: int
8705 fd_high: int
8706 /
8707
8708Closes all file descriptors in [fd_low, fd_high), ignoring errors.
8709[clinic start generated code]*/
8710
Larry Hastings2f936352014-08-05 14:04:04 +10008711static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008712os_closerange_impl(PyObject *module, int fd_low, int fd_high)
8713/*[clinic end generated code: output=0ce5c20fcda681c2 input=5855a3d053ebd4ec]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008714{
Jakub Kulíke20134f2019-09-11 17:11:57 +02008715#ifdef HAVE_FDWALK
8716 int lohi[2];
Jakub Kulíke20134f2019-09-11 17:11:57 +02008717#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00008718 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008719 _Py_BEGIN_SUPPRESS_IPH
Jakub Kulíke20134f2019-09-11 17:11:57 +02008720#ifdef HAVE_FDWALK
8721 lohi[0] = Py_MAX(fd_low, 0);
8722 lohi[1] = fd_high;
8723 fdwalk(_fdwalk_close_func, lohi);
8724#else
Victor Stinner162c5672020-04-24 12:00:51 +02008725 fd_low = Py_MAX(fd_low, 0);
8726#ifdef __FreeBSD__
8727 if (fd_high >= sysconf(_SC_OPEN_MAX)) {
8728 /* Any errors encountered while closing file descriptors are ignored */
8729 closefrom(fd_low);
8730 }
8731 else
8732#endif
8733 {
8734 for (int i = fd_low; i < fd_high; i++) {
8735 /* Ignore errors */
8736 (void)close(i);
8737 }
8738 }
Jakub Kulíke20134f2019-09-11 17:11:57 +02008739#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008740 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008741 Py_END_ALLOW_THREADS
8742 Py_RETURN_NONE;
Christian Heimesfdab48e2008-01-20 09:06:41 +00008743}
8744
8745
Larry Hastings2f936352014-08-05 14:04:04 +10008746/*[clinic input]
8747os.dup -> int
8748
8749 fd: int
8750 /
8751
8752Return a duplicate of a file descriptor.
8753[clinic start generated code]*/
8754
Larry Hastings2f936352014-08-05 14:04:04 +10008755static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008756os_dup_impl(PyObject *module, int fd)
8757/*[clinic end generated code: output=486f4860636b2a9f input=6f10f7ea97f7852a]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008758{
8759 return _Py_dup(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +00008760}
8761
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008762
Larry Hastings2f936352014-08-05 14:04:04 +10008763/*[clinic input]
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008764os.dup2 -> int
Larry Hastings2f936352014-08-05 14:04:04 +10008765 fd: int
8766 fd2: int
8767 inheritable: bool=True
8768
8769Duplicate file descriptor.
8770[clinic start generated code]*/
8771
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008772static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008773os_dup2_impl(PyObject *module, int fd, int fd2, int inheritable)
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008774/*[clinic end generated code: output=bc059d34a73404d1 input=c3cddda8922b038d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008775{
Stéphane Wirtel3d86e482018-01-30 07:04:36 +01008776 int res = 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008777#if defined(HAVE_DUP3) && \
8778 !(defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC))
8779 /* dup3() is available on Linux 2.6.27+ and glibc 2.9 */
Alexey Izbyshevb3caf382018-02-20 10:25:46 +03008780 static int dup3_works = -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008781#endif
8782
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008783 if (fd < 0 || fd2 < 0) {
8784 posix_error();
8785 return -1;
8786 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008787
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008788 /* dup2() can fail with EINTR if the target FD is already open, because it
8789 * then has to be closed. See os_close_impl() for why we don't handle EINTR
8790 * upon close(), and therefore below.
8791 */
Victor Stinnerdaf45552013-08-28 00:53:59 +02008792#ifdef MS_WINDOWS
8793 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008794 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008795 res = dup2(fd, fd2);
Steve Dower8fc89802015-04-12 00:26:27 -04008796 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02008797 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008798 if (res < 0) {
8799 posix_error();
8800 return -1;
8801 }
8802 res = fd2; // msvcrt dup2 returns 0 on success.
Victor Stinnerdaf45552013-08-28 00:53:59 +02008803
8804 /* Character files like console cannot be make non-inheritable */
8805 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8806 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008807 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008808 }
8809
8810#elif defined(HAVE_FCNTL_H) && defined(F_DUP2FD_CLOEXEC)
8811 Py_BEGIN_ALLOW_THREADS
8812 if (!inheritable)
8813 res = fcntl(fd, F_DUP2FD_CLOEXEC, fd2);
8814 else
8815 res = dup2(fd, fd2);
8816 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008817 if (res < 0) {
8818 posix_error();
8819 return -1;
8820 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008821
8822#else
8823
8824#ifdef HAVE_DUP3
8825 if (!inheritable && dup3_works != 0) {
8826 Py_BEGIN_ALLOW_THREADS
8827 res = dup3(fd, fd2, O_CLOEXEC);
8828 Py_END_ALLOW_THREADS
8829 if (res < 0) {
8830 if (dup3_works == -1)
8831 dup3_works = (errno != ENOSYS);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008832 if (dup3_works) {
8833 posix_error();
8834 return -1;
8835 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008836 }
8837 }
8838
8839 if (inheritable || dup3_works == 0)
8840 {
8841#endif
8842 Py_BEGIN_ALLOW_THREADS
8843 res = dup2(fd, fd2);
8844 Py_END_ALLOW_THREADS
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008845 if (res < 0) {
8846 posix_error();
8847 return -1;
8848 }
Victor Stinnerdaf45552013-08-28 00:53:59 +02008849
8850 if (!inheritable && _Py_set_inheritable(fd2, 0, NULL) < 0) {
8851 close(fd2);
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008852 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +02008853 }
8854#ifdef HAVE_DUP3
8855 }
8856#endif
8857
8858#endif
8859
Benjamin Petersonbbdb17d2017-12-29 13:13:06 -08008860 return res;
Guido van Rossum687dd131993-05-17 08:34:16 +00008861}
8862
Larry Hastings2f936352014-08-05 14:04:04 +10008863
Ross Lagerwall7807c352011-03-17 20:20:30 +02008864#ifdef HAVE_LOCKF
Larry Hastings2f936352014-08-05 14:04:04 +10008865/*[clinic input]
8866os.lockf
8867
8868 fd: int
8869 An open file descriptor.
8870 command: int
8871 One of F_LOCK, F_TLOCK, F_ULOCK or F_TEST.
8872 length: Py_off_t
8873 The number of bytes to lock, starting at the current position.
8874 /
8875
8876Apply, test or remove a POSIX lock on an open file descriptor.
8877
8878[clinic start generated code]*/
8879
Larry Hastings2f936352014-08-05 14:04:04 +10008880static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008881os_lockf_impl(PyObject *module, int fd, int command, Py_off_t length)
8882/*[clinic end generated code: output=af7051f3e7c29651 input=65da41d2106e9b79]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008883{
8884 int res;
Ross Lagerwall7807c352011-03-17 20:20:30 +02008885
Saiyang Gou7514f4f2020-02-12 23:47:42 -08008886 if (PySys_Audit("os.lockf", "iiL", fd, command, length) < 0) {
8887 return NULL;
8888 }
8889
Ross Lagerwall7807c352011-03-17 20:20:30 +02008890 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008891 res = lockf(fd, command, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02008892 Py_END_ALLOW_THREADS
8893
8894 if (res < 0)
8895 return posix_error();
8896
8897 Py_RETURN_NONE;
8898}
Larry Hastings2f936352014-08-05 14:04:04 +10008899#endif /* HAVE_LOCKF */
Ross Lagerwall7807c352011-03-17 20:20:30 +02008900
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008901
Larry Hastings2f936352014-08-05 14:04:04 +10008902/*[clinic input]
8903os.lseek -> Py_off_t
8904
8905 fd: int
8906 position: Py_off_t
8907 how: int
8908 /
8909
8910Set the position of a file descriptor. Return the new position.
8911
8912Return the new cursor position in number of bytes
8913relative to the beginning of the file.
8914[clinic start generated code]*/
8915
Larry Hastings2f936352014-08-05 14:04:04 +10008916static Py_off_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008917os_lseek_impl(PyObject *module, int fd, Py_off_t position, int how)
8918/*[clinic end generated code: output=971e1efb6b30bd2f input=902654ad3f96a6d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008919{
8920 Py_off_t result;
8921
Guido van Rossum687dd131993-05-17 08:34:16 +00008922#ifdef SEEK_SET
Victor Stinner8c62be82010-05-06 00:08:46 +00008923 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
8924 switch (how) {
Larry Hastings2f936352014-08-05 14:04:04 +10008925 case 0: how = SEEK_SET; break;
8926 case 1: how = SEEK_CUR; break;
8927 case 2: how = SEEK_END; break;
Victor Stinner8c62be82010-05-06 00:08:46 +00008928 }
Guido van Rossum6a3eb5f1994-08-18 15:42:46 +00008929#endif /* SEEK_END */
Guido van Rossum94f6f721999-01-06 18:42:14 +00008930
Victor Stinner8c62be82010-05-06 00:08:46 +00008931 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04008932 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner14b9b112013-06-25 00:37:25 +02008933#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +10008934 result = _lseeki64(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008935#else
Larry Hastings2f936352014-08-05 14:04:04 +10008936 result = lseek(fd, position, how);
Fred Drake699f3522000-06-29 21:12:41 +00008937#endif
Steve Dower8fc89802015-04-12 00:26:27 -04008938 _Py_END_SUPPRESS_IPH
Victor Stinner8c62be82010-05-06 00:08:46 +00008939 Py_END_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10008940 if (result < 0)
8941 posix_error();
Guido van Rossum94f6f721999-01-06 18:42:14 +00008942
Larry Hastings2f936352014-08-05 14:04:04 +10008943 return result;
Guido van Rossum687dd131993-05-17 08:34:16 +00008944}
8945
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00008946
Larry Hastings2f936352014-08-05 14:04:04 +10008947/*[clinic input]
8948os.read
8949 fd: int
8950 length: Py_ssize_t
8951 /
8952
8953Read from a file descriptor. Returns a bytes object.
8954[clinic start generated code]*/
8955
Larry Hastings2f936352014-08-05 14:04:04 +10008956static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03008957os_read_impl(PyObject *module, int fd, Py_ssize_t length)
8958/*[clinic end generated code: output=dafbe9a5cddb987b input=1df2eaa27c0bf1d3]*/
Larry Hastings2f936352014-08-05 14:04:04 +10008959{
Victor Stinner8c62be82010-05-06 00:08:46 +00008960 Py_ssize_t n;
8961 PyObject *buffer;
Larry Hastings2f936352014-08-05 14:04:04 +10008962
8963 if (length < 0) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008964 errno = EINVAL;
8965 return posix_error();
8966 }
Larry Hastings2f936352014-08-05 14:04:04 +10008967
Victor Stinner9a0d7a72018-11-22 15:03:40 +01008968 length = Py_MIN(length, _PY_READ_MAX);
Larry Hastings2f936352014-08-05 14:04:04 +10008969
8970 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Victor Stinner8c62be82010-05-06 00:08:46 +00008971 if (buffer == NULL)
8972 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00008973
Victor Stinner66aab0c2015-03-19 22:53:20 +01008974 n = _Py_read(fd, PyBytes_AS_STRING(buffer), length);
8975 if (n == -1) {
Victor Stinner8c62be82010-05-06 00:08:46 +00008976 Py_DECREF(buffer);
Victor Stinner66aab0c2015-03-19 22:53:20 +01008977 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +00008978 }
Larry Hastings2f936352014-08-05 14:04:04 +10008979
8980 if (n != length)
Victor Stinner8c62be82010-05-06 00:08:46 +00008981 _PyBytes_Resize(&buffer, n);
Larry Hastings2f936352014-08-05 14:04:04 +10008982
Victor Stinner8c62be82010-05-06 00:08:46 +00008983 return buffer;
Guido van Rossum687dd131993-05-17 08:34:16 +00008984}
8985
Ross Lagerwall7807c352011-03-17 20:20:30 +02008986#if (defined(HAVE_SENDFILE) && (defined(__FreeBSD__) || defined(__DragonFly__) \
Serhiy Storchaka9d572732018-07-31 10:24:54 +03008987 || defined(__APPLE__))) \
8988 || defined(HAVE_READV) || defined(HAVE_PREADV) || defined (HAVE_PREADV2) \
8989 || defined(HAVE_WRITEV) || defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
8990static int
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008991iov_setup(struct iovec **iov, Py_buffer **buf, PyObject *seq, Py_ssize_t cnt, int type)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008992{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03008993 Py_ssize_t i, j;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00008994
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008995 *iov = PyMem_New(struct iovec, cnt);
8996 if (*iov == NULL) {
8997 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01008998 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00008999 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009000
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009001 *buf = PyMem_New(Py_buffer, cnt);
9002 if (*buf == NULL) {
9003 PyMem_Del(*iov);
9004 PyErr_NoMemory();
Victor Stinner57ddf782014-01-08 15:21:28 +01009005 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009006 }
9007
9008 for (i = 0; i < cnt; i++) {
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009009 PyObject *item = PySequence_GetItem(seq, i);
9010 if (item == NULL)
9011 goto fail;
9012 if (PyObject_GetBuffer(item, &(*buf)[i], type) == -1) {
9013 Py_DECREF(item);
9014 goto fail;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009015 }
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009016 Py_DECREF(item);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009017 (*iov)[i].iov_base = (*buf)[i].buf;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009018 (*iov)[i].iov_len = (*buf)[i].len;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009019 }
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009020 return 0;
Ross Lagerwall9ad63e02011-03-19 09:11:14 +02009021
9022fail:
9023 PyMem_Del(*iov);
9024 for (j = 0; j < i; j++) {
9025 PyBuffer_Release(&(*buf)[j]);
9026 }
9027 PyMem_Del(*buf);
Victor Stinner57ddf782014-01-08 15:21:28 +01009028 return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009029}
9030
9031static void
9032iov_cleanup(struct iovec *iov, Py_buffer *buf, int cnt)
9033{
9034 int i;
9035 PyMem_Del(iov);
9036 for (i = 0; i < cnt; i++) {
9037 PyBuffer_Release(&buf[i]);
9038 }
9039 PyMem_Del(buf);
9040}
9041#endif
9042
Larry Hastings2f936352014-08-05 14:04:04 +10009043
Ross Lagerwall7807c352011-03-17 20:20:30 +02009044#ifdef HAVE_READV
Larry Hastings2f936352014-08-05 14:04:04 +10009045/*[clinic input]
9046os.readv -> Py_ssize_t
9047
9048 fd: int
9049 buffers: object
9050 /
9051
9052Read from a file descriptor fd into an iterable of buffers.
9053
9054The buffers should be mutable buffers accepting bytes.
9055readv will transfer data into each buffer until it is full
9056and then move on to the next buffer in the sequence to hold
9057the rest of the data.
9058
9059readv returns the total number of bytes read,
9060which may be less than the total capacity of all the buffers.
9061[clinic start generated code]*/
9062
Larry Hastings2f936352014-08-05 14:04:04 +10009063static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009064os_readv_impl(PyObject *module, int fd, PyObject *buffers)
9065/*[clinic end generated code: output=792da062d3fcebdb input=e679eb5dbfa0357d]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009066{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009067 Py_ssize_t cnt, n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009068 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009069 struct iovec *iov;
9070 Py_buffer *buf;
9071
Larry Hastings2f936352014-08-05 14:04:04 +10009072 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009073 PyErr_SetString(PyExc_TypeError,
9074 "readv() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009075 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009076 }
Ross Lagerwall7807c352011-03-17 20:20:30 +02009077
Larry Hastings2f936352014-08-05 14:04:04 +10009078 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009079 if (cnt < 0)
9080 return -1;
Larry Hastings2f936352014-08-05 14:04:04 +10009081
9082 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0)
9083 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009084
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009085 do {
9086 Py_BEGIN_ALLOW_THREADS
9087 n = readv(fd, iov, cnt);
9088 Py_END_ALLOW_THREADS
9089 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009090
9091 iov_cleanup(iov, buf, cnt);
Larry Hastings2f936352014-08-05 14:04:04 +10009092 if (n < 0) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009093 if (!async_err)
9094 posix_error();
Larry Hastings2f936352014-08-05 14:04:04 +10009095 return -1;
9096 }
Victor Stinner57ddf782014-01-08 15:21:28 +01009097
Larry Hastings2f936352014-08-05 14:04:04 +10009098 return n;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009099}
Larry Hastings2f936352014-08-05 14:04:04 +10009100#endif /* HAVE_READV */
9101
Ross Lagerwall7807c352011-03-17 20:20:30 +02009102
9103#ifdef HAVE_PREAD
Larry Hastings2f936352014-08-05 14:04:04 +10009104/*[clinic input]
Larry Hastings2f936352014-08-05 14:04:04 +10009105os.pread
9106
9107 fd: int
Dong-hee Naad7736f2019-09-25 14:47:04 +09009108 length: Py_ssize_t
Larry Hastings2f936352014-08-05 14:04:04 +10009109 offset: Py_off_t
9110 /
9111
9112Read a number of bytes from a file descriptor starting at a particular offset.
9113
9114Read length bytes from file descriptor fd, starting at offset bytes from
9115the beginning of the file. The file offset remains unchanged.
9116[clinic start generated code]*/
9117
Larry Hastings2f936352014-08-05 14:04:04 +10009118static PyObject *
Dong-hee Naad7736f2019-09-25 14:47:04 +09009119os_pread_impl(PyObject *module, int fd, Py_ssize_t length, Py_off_t offset)
9120/*[clinic end generated code: output=3f875c1eef82e32f input=85cb4a5589627144]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009121{
Ross Lagerwall7807c352011-03-17 20:20:30 +02009122 Py_ssize_t n;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009123 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009124 PyObject *buffer;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009125
Larry Hastings2f936352014-08-05 14:04:04 +10009126 if (length < 0) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009127 errno = EINVAL;
9128 return posix_error();
9129 }
Larry Hastings2f936352014-08-05 14:04:04 +10009130 buffer = PyBytes_FromStringAndSize((char *)NULL, length);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009131 if (buffer == NULL)
9132 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009133
9134 do {
9135 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009136 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009137 n = pread(fd, PyBytes_AS_STRING(buffer), length, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009138 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009139 Py_END_ALLOW_THREADS
9140 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9141
Ross Lagerwall7807c352011-03-17 20:20:30 +02009142 if (n < 0) {
9143 Py_DECREF(buffer);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009144 return (!async_err) ? posix_error() : NULL;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009145 }
Larry Hastings2f936352014-08-05 14:04:04 +10009146 if (n != length)
Ross Lagerwall7807c352011-03-17 20:20:30 +02009147 _PyBytes_Resize(&buffer, n);
9148 return buffer;
9149}
Larry Hastings2f936352014-08-05 14:04:04 +10009150#endif /* HAVE_PREAD */
Ross Lagerwall7807c352011-03-17 20:20:30 +02009151
Pablo Galindo4defba32018-01-27 16:16:37 +00009152#if defined(HAVE_PREADV) || defined (HAVE_PREADV2)
9153/*[clinic input]
9154os.preadv -> Py_ssize_t
9155
9156 fd: int
9157 buffers: object
9158 offset: Py_off_t
9159 flags: int = 0
9160 /
9161
9162Reads from a file descriptor into a number of mutable bytes-like objects.
9163
9164Combines the functionality of readv() and pread(). As readv(), it will
9165transfer data into each buffer until it is full and then move on to the next
9166buffer in the sequence to hold the rest of the data. Its fourth argument,
9167specifies the file offset at which the input operation is to be performed. It
9168will return the total number of bytes read (which can be less than the total
9169capacity of all the objects).
9170
9171The flags argument contains a bitwise OR of zero or more of the following flags:
9172
9173- RWF_HIPRI
9174- RWF_NOWAIT
9175
9176Using non-zero flags requires Linux 4.6 or newer.
9177[clinic start generated code]*/
9178
9179static Py_ssize_t
9180os_preadv_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9181 int flags)
9182/*[clinic end generated code: output=26fc9c6e58e7ada5 input=4173919dc1f7ed99]*/
9183{
9184 Py_ssize_t cnt, n;
9185 int async_err = 0;
9186 struct iovec *iov;
9187 Py_buffer *buf;
9188
9189 if (!PySequence_Check(buffers)) {
9190 PyErr_SetString(PyExc_TypeError,
9191 "preadv2() arg 2 must be a sequence");
9192 return -1;
9193 }
9194
9195 cnt = PySequence_Size(buffers);
9196 if (cnt < 0) {
9197 return -1;
9198 }
9199
9200#ifndef HAVE_PREADV2
9201 if(flags != 0) {
9202 argument_unavailable_error("preadv2", "flags");
9203 return -1;
9204 }
9205#endif
9206
9207 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_WRITABLE) < 0) {
9208 return -1;
9209 }
9210#ifdef HAVE_PREADV2
9211 do {
9212 Py_BEGIN_ALLOW_THREADS
9213 _Py_BEGIN_SUPPRESS_IPH
9214 n = preadv2(fd, iov, cnt, offset, flags);
9215 _Py_END_SUPPRESS_IPH
9216 Py_END_ALLOW_THREADS
9217 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9218#else
9219 do {
9220 Py_BEGIN_ALLOW_THREADS
9221 _Py_BEGIN_SUPPRESS_IPH
9222 n = preadv(fd, iov, cnt, offset);
9223 _Py_END_SUPPRESS_IPH
9224 Py_END_ALLOW_THREADS
9225 } while (n < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9226#endif
9227
9228 iov_cleanup(iov, buf, cnt);
9229 if (n < 0) {
9230 if (!async_err) {
9231 posix_error();
9232 }
9233 return -1;
9234 }
9235
9236 return n;
9237}
9238#endif /* HAVE_PREADV */
9239
Larry Hastings2f936352014-08-05 14:04:04 +10009240
9241/*[clinic input]
9242os.write -> Py_ssize_t
9243
9244 fd: int
9245 data: Py_buffer
9246 /
9247
9248Write a bytes object to a file descriptor.
9249[clinic start generated code]*/
9250
Larry Hastings2f936352014-08-05 14:04:04 +10009251static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009252os_write_impl(PyObject *module, int fd, Py_buffer *data)
9253/*[clinic end generated code: output=e4ef5bc904b58ef9 input=3207e28963234f3c]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009254{
Victor Stinner66aab0c2015-03-19 22:53:20 +01009255 return _Py_write(fd, data->buf, data->len);
Ross Lagerwall7807c352011-03-17 20:20:30 +02009256}
9257
9258#ifdef HAVE_SENDFILE
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009259#ifdef __APPLE__
9260/*[clinic input]
9261os.sendfile
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009262
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009263 out_fd: int
9264 in_fd: int
9265 offset: Py_off_t
9266 count as sbytes: Py_off_t
9267 headers: object(c_default="NULL") = ()
9268 trailers: object(c_default="NULL") = ()
9269 flags: int = 0
9270
9271Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9272[clinic start generated code]*/
9273
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009274static PyObject *
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009275os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
9276 Py_off_t sbytes, PyObject *headers, PyObject *trailers,
9277 int flags)
9278/*[clinic end generated code: output=81c4bcd143f5c82b input=b0d72579d4c69afa]*/
9279#elif defined(__FreeBSD__) || defined(__DragonFly__)
9280/*[clinic input]
9281os.sendfile
9282
9283 out_fd: int
9284 in_fd: int
9285 offset: Py_off_t
9286 count: Py_ssize_t
9287 headers: object(c_default="NULL") = ()
9288 trailers: object(c_default="NULL") = ()
9289 flags: int = 0
9290
9291Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9292[clinic start generated code]*/
9293
9294static PyObject *
9295os_sendfile_impl(PyObject *module, int out_fd, int in_fd, Py_off_t offset,
9296 Py_ssize_t count, PyObject *headers, PyObject *trailers,
9297 int flags)
9298/*[clinic end generated code: output=329ea009bdd55afc input=338adb8ff84ae8cd]*/
9299#else
9300/*[clinic input]
9301os.sendfile
9302
9303 out_fd: int
9304 in_fd: int
9305 offset as offobj: object
9306 count: Py_ssize_t
9307
9308Copy count bytes from file descriptor in_fd to file descriptor out_fd.
9309[clinic start generated code]*/
9310
9311static PyObject *
9312os_sendfile_impl(PyObject *module, int out_fd, int in_fd, PyObject *offobj,
9313 Py_ssize_t count)
9314/*[clinic end generated code: output=ae81216e40f167d8 input=76d64058c74477ba]*/
9315#endif
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009316{
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009317 Py_ssize_t ret;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009318 int async_err = 0;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009319
9320#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
9321#ifndef __APPLE__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009322 off_t sbytes;
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009323#endif
9324 Py_buffer *hbuf, *tbuf;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009325 struct sf_hdtr sf;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009326
Victor Stinner6ce0dbf2013-07-07 16:32:36 +02009327 sf.headers = NULL;
9328 sf.trailers = NULL;
9329
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009330 if (headers != NULL) {
9331 if (!PySequence_Check(headers)) {
9332 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009333 "sendfile() headers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009334 return NULL;
9335 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009336 Py_ssize_t i = PySequence_Size(headers);
9337 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009338 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009339 if (i > INT_MAX) {
9340 PyErr_SetString(PyExc_OverflowError,
9341 "sendfile() header is too large");
9342 return NULL;
9343 }
9344 if (i > 0) {
9345 sf.hdr_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009346 if (iov_setup(&(sf.headers), &hbuf,
9347 headers, sf.hdr_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009348 return NULL;
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009349#ifdef __APPLE__
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009350 for (i = 0; i < sf.hdr_cnt; i++) {
9351 Py_ssize_t blen = sf.headers[i].iov_len;
9352# define OFF_T_MAX 0x7fffffffffffffff
9353 if (sbytes >= OFF_T_MAX - blen) {
9354 PyErr_SetString(PyExc_OverflowError,
9355 "sendfile() header is too large");
9356 return NULL;
9357 }
9358 sbytes += blen;
9359 }
Giampaolo Rodolàacdad9a2011-03-03 16:10:51 +00009360#endif
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009361 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009362 }
9363 }
9364 if (trailers != NULL) {
9365 if (!PySequence_Check(trailers)) {
9366 PyErr_SetString(PyExc_TypeError,
Martin Panter94994132015-09-09 05:29:24 +00009367 "sendfile() trailers must be a sequence");
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009368 return NULL;
9369 } else {
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009370 Py_ssize_t i = PySequence_Size(trailers);
9371 if (i < 0)
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009372 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009373 if (i > INT_MAX) {
9374 PyErr_SetString(PyExc_OverflowError,
9375 "sendfile() trailer is too large");
9376 return NULL;
9377 }
9378 if (i > 0) {
9379 sf.trl_cnt = (int)i;
Serhiy Storchaka9d572732018-07-31 10:24:54 +03009380 if (iov_setup(&(sf.trailers), &tbuf,
9381 trailers, sf.trl_cnt, PyBUF_SIMPLE) < 0)
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009382 return NULL;
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009383 }
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009384 }
9385 }
9386
Steve Dower8fc89802015-04-12 00:26:27 -04009387 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009388 do {
9389 Py_BEGIN_ALLOW_THREADS
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009390#ifdef __APPLE__
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009391 ret = sendfile(in_fd, out_fd, offset, &sbytes, &sf, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009392#else
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009393 ret = sendfile(in_fd, out_fd, offset, count, &sf, &sbytes, flags);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009394#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009395 Py_END_ALLOW_THREADS
9396 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Steve Dower8fc89802015-04-12 00:26:27 -04009397 _Py_END_SUPPRESS_IPH
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009398
9399 if (sf.headers != NULL)
9400 iov_cleanup(sf.headers, hbuf, sf.hdr_cnt);
9401 if (sf.trailers != NULL)
9402 iov_cleanup(sf.trailers, tbuf, sf.trl_cnt);
9403
9404 if (ret < 0) {
9405 if ((errno == EAGAIN) || (errno == EBUSY)) {
9406 if (sbytes != 0) {
9407 // some data has been sent
9408 goto done;
9409 }
9410 else {
9411 // no data has been sent; upper application is supposed
9412 // to retry on EAGAIN or EBUSY
9413 return posix_error();
9414 }
9415 }
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009416 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009417 }
9418 goto done;
9419
9420done:
9421 #if !defined(HAVE_LARGEFILE_SUPPORT)
9422 return Py_BuildValue("l", sbytes);
9423 #else
9424 return Py_BuildValue("L", sbytes);
9425 #endif
9426
9427#else
Benjamin Peterson840ef8f2016-09-07 14:45:10 -07009428#ifdef __linux__
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009429 if (offobj == Py_None) {
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009430 do {
9431 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009432 ret = sendfile(out_fd, in_fd, NULL, count);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009433 Py_END_ALLOW_THREADS
9434 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009435 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009436 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodola'ff1a7352011-04-19 09:47:16 +02009437 return Py_BuildValue("n", ret);
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009438 }
9439#endif
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009440 off_t offset;
Larry Hastings2f936352014-08-05 14:04:04 +10009441 if (!Py_off_t_converter(offobj, &offset))
Antoine Pitroudcc20b82011-02-26 13:38:35 +00009442 return NULL;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009443
9444 do {
9445 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka2b560312020-04-18 19:14:10 +03009446 ret = sendfile(out_fd, in_fd, &offset, count);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009447 Py_END_ALLOW_THREADS
9448 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009449 if (ret < 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009450 return (!async_err) ? posix_error() : NULL;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +00009451 return Py_BuildValue("n", ret);
9452#endif
9453}
Larry Hastings2f936352014-08-05 14:04:04 +10009454#endif /* HAVE_SENDFILE */
Guido van Rossumec4f4ac1997-06-02 22:20:51 +00009455
Larry Hastings2f936352014-08-05 14:04:04 +10009456
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009457#if defined(__APPLE__)
9458/*[clinic input]
9459os._fcopyfile
9460
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009461 in_fd: int
9462 out_fd: int
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009463 flags: int
9464 /
9465
Giampaolo Rodolac7f02a92018-06-19 08:27:29 -07009466Efficiently copy content or metadata of 2 regular file descriptors (macOS).
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009467[clinic start generated code]*/
9468
9469static PyObject *
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009470os__fcopyfile_impl(PyObject *module, int in_fd, int out_fd, int flags)
9471/*[clinic end generated code: output=c9d1a35a992e401b input=1e34638a86948795]*/
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009472{
9473 int ret;
9474
9475 Py_BEGIN_ALLOW_THREADS
Serhiy Storchaka140a7d12019-10-13 11:59:31 +03009476 ret = fcopyfile(in_fd, out_fd, NULL, flags);
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +02009477 Py_END_ALLOW_THREADS
9478 if (ret < 0)
9479 return posix_error();
9480 Py_RETURN_NONE;
9481}
9482#endif
9483
9484
Larry Hastings2f936352014-08-05 14:04:04 +10009485/*[clinic input]
9486os.fstat
9487
9488 fd : int
9489
9490Perform a stat system call on the given file descriptor.
9491
9492Like stat(), but for an open file descriptor.
9493Equivalent to os.stat(fd).
9494[clinic start generated code]*/
9495
Larry Hastings2f936352014-08-05 14:04:04 +10009496static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009497os_fstat_impl(PyObject *module, int fd)
9498/*[clinic end generated code: output=efc038cb5f654492 input=27e0e0ebbe5600c9]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009499{
Victor Stinner8c62be82010-05-06 00:08:46 +00009500 STRUCT_STAT st;
9501 int res;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009502 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009503
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009504 do {
9505 Py_BEGIN_ALLOW_THREADS
9506 res = FSTAT(fd, &st);
9507 Py_END_ALLOW_THREADS
9508 } while (res != 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Victor Stinner8c62be82010-05-06 00:08:46 +00009509 if (res != 0) {
Martin v. Löwis14694662006-02-03 12:54:16 +00009510#ifdef MS_WINDOWS
Victor Stinnerb024e842012-10-31 22:24:06 +01009511 return PyErr_SetFromWindowsErr(0);
Martin v. Löwis14694662006-02-03 12:54:16 +00009512#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009513 return (!async_err) ? posix_error() : NULL;
Martin v. Löwis14694662006-02-03 12:54:16 +00009514#endif
Victor Stinner8c62be82010-05-06 00:08:46 +00009515 }
Tim Peters5aa91602002-01-30 05:46:57 +00009516
Victor Stinner4195b5c2012-02-08 23:03:19 +01009517 return _pystat_fromstructstat(&st);
Guido van Rossum687dd131993-05-17 08:34:16 +00009518}
9519
Larry Hastings2f936352014-08-05 14:04:04 +10009520
9521/*[clinic input]
9522os.isatty -> bool
9523 fd: int
9524 /
9525
9526Return True if the fd is connected to a terminal.
9527
9528Return True if the file descriptor is an open file descriptor
9529connected to the slave end of a terminal.
9530[clinic start generated code]*/
9531
Larry Hastings2f936352014-08-05 14:04:04 +10009532static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009533os_isatty_impl(PyObject *module, int fd)
9534/*[clinic end generated code: output=6a48c8b4e644ca00 input=08ce94aa1eaf7b5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009535{
Steve Dower8fc89802015-04-12 00:26:27 -04009536 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -04009537 _Py_BEGIN_SUPPRESS_IPH
9538 return_value = isatty(fd);
9539 _Py_END_SUPPRESS_IPH
9540 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +10009541}
9542
9543
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009544#ifdef HAVE_PIPE
Larry Hastings2f936352014-08-05 14:04:04 +10009545/*[clinic input]
9546os.pipe
9547
9548Create a pipe.
9549
9550Returns a tuple of two file descriptors:
9551 (read_fd, write_fd)
9552[clinic start generated code]*/
9553
Larry Hastings2f936352014-08-05 14:04:04 +10009554static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009555os_pipe_impl(PyObject *module)
9556/*[clinic end generated code: output=ff9b76255793b440 input=02535e8c8fa6c4d4]*/
Guido van Rossum687dd131993-05-17 08:34:16 +00009557{
Victor Stinner8c62be82010-05-06 00:08:46 +00009558 int fds[2];
Victor Stinnerdaf45552013-08-28 00:53:59 +02009559#ifdef MS_WINDOWS
Victor Stinner8c62be82010-05-06 00:08:46 +00009560 HANDLE read, write;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009561 SECURITY_ATTRIBUTES attr;
Victor Stinner8c62be82010-05-06 00:08:46 +00009562 BOOL ok;
Victor Stinnerdaf45552013-08-28 00:53:59 +02009563#else
9564 int res;
9565#endif
9566
9567#ifdef MS_WINDOWS
9568 attr.nLength = sizeof(attr);
9569 attr.lpSecurityDescriptor = NULL;
9570 attr.bInheritHandle = FALSE;
9571
9572 Py_BEGIN_ALLOW_THREADS
Steve Dowerc3630612016-11-19 18:41:16 -08009573 _Py_BEGIN_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009574 ok = CreatePipe(&read, &write, &attr, 0);
9575 if (ok) {
Benjamin Petersonca470632016-09-06 13:47:26 -07009576 fds[0] = _open_osfhandle((intptr_t)read, _O_RDONLY);
9577 fds[1] = _open_osfhandle((intptr_t)write, _O_WRONLY);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009578 if (fds[0] == -1 || fds[1] == -1) {
9579 CloseHandle(read);
9580 CloseHandle(write);
9581 ok = 0;
9582 }
9583 }
Steve Dowerc3630612016-11-19 18:41:16 -08009584 _Py_END_SUPPRESS_IPH
Victor Stinnerdaf45552013-08-28 00:53:59 +02009585 Py_END_ALLOW_THREADS
9586
Victor Stinner8c62be82010-05-06 00:08:46 +00009587 if (!ok)
Victor Stinnerb024e842012-10-31 22:24:06 +01009588 return PyErr_SetFromWindowsErr(0);
Victor Stinnerdaf45552013-08-28 00:53:59 +02009589#else
9590
9591#ifdef HAVE_PIPE2
9592 Py_BEGIN_ALLOW_THREADS
9593 res = pipe2(fds, O_CLOEXEC);
9594 Py_END_ALLOW_THREADS
9595
9596 if (res != 0 && errno == ENOSYS)
9597 {
9598#endif
9599 Py_BEGIN_ALLOW_THREADS
9600 res = pipe(fds);
9601 Py_END_ALLOW_THREADS
9602
9603 if (res == 0) {
9604 if (_Py_set_inheritable(fds[0], 0, NULL) < 0) {
9605 close(fds[0]);
9606 close(fds[1]);
9607 return NULL;
9608 }
9609 if (_Py_set_inheritable(fds[1], 0, NULL) < 0) {
9610 close(fds[0]);
9611 close(fds[1]);
9612 return NULL;
9613 }
9614 }
9615#ifdef HAVE_PIPE2
9616 }
9617#endif
9618
9619 if (res != 0)
9620 return PyErr_SetFromErrno(PyExc_OSError);
9621#endif /* !MS_WINDOWS */
9622 return Py_BuildValue("(ii)", fds[0], fds[1]);
Guido van Rossum687dd131993-05-17 08:34:16 +00009623}
Guido van Rossuma4916fa1996-05-23 22:58:55 +00009624#endif /* HAVE_PIPE */
9625
Larry Hastings2f936352014-08-05 14:04:04 +10009626
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009627#ifdef HAVE_PIPE2
Larry Hastings2f936352014-08-05 14:04:04 +10009628/*[clinic input]
9629os.pipe2
9630
9631 flags: int
9632 /
9633
9634Create a pipe with flags set atomically.
9635
9636Returns a tuple of two file descriptors:
9637 (read_fd, write_fd)
9638
9639flags can be constructed by ORing together one or more of these values:
9640O_NONBLOCK, O_CLOEXEC.
9641[clinic start generated code]*/
9642
Larry Hastings2f936352014-08-05 14:04:04 +10009643static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009644os_pipe2_impl(PyObject *module, int flags)
9645/*[clinic end generated code: output=25751fb43a45540f input=f261b6e7e63c6817]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009646{
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009647 int fds[2];
9648 int res;
9649
Charles-François Natalidaafdd52011-05-29 20:07:40 +02009650 res = pipe2(fds, flags);
9651 if (res != 0)
9652 return posix_error();
9653 return Py_BuildValue("(ii)", fds[0], fds[1]);
9654}
9655#endif /* HAVE_PIPE2 */
9656
Larry Hastings2f936352014-08-05 14:04:04 +10009657
Ross Lagerwall7807c352011-03-17 20:20:30 +02009658#ifdef HAVE_WRITEV
Larry Hastings2f936352014-08-05 14:04:04 +10009659/*[clinic input]
9660os.writev -> Py_ssize_t
9661 fd: int
9662 buffers: object
9663 /
9664
9665Iterate over buffers, and write the contents of each to a file descriptor.
9666
9667Returns the total number of bytes written.
9668buffers must be a sequence of bytes-like objects.
9669[clinic start generated code]*/
9670
Larry Hastings2f936352014-08-05 14:04:04 +10009671static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009672os_writev_impl(PyObject *module, int fd, PyObject *buffers)
9673/*[clinic end generated code: output=56565cfac3aac15b input=5b8d17fe4189d2fe]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009674{
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009675 Py_ssize_t cnt;
Larry Hastings2f936352014-08-05 14:04:04 +10009676 Py_ssize_t result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009677 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009678 struct iovec *iov;
9679 Py_buffer *buf;
Larry Hastings2f936352014-08-05 14:04:04 +10009680
9681 if (!PySequence_Check(buffers)) {
Ross Lagerwall7807c352011-03-17 20:20:30 +02009682 PyErr_SetString(PyExc_TypeError,
9683 "writev() arg 2 must be a sequence");
Larry Hastings2f936352014-08-05 14:04:04 +10009684 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009685 }
Larry Hastings2f936352014-08-05 14:04:04 +10009686 cnt = PySequence_Size(buffers);
Serhiy Storchakabf623ae2017-04-19 20:03:52 +03009687 if (cnt < 0)
9688 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009689
Larry Hastings2f936352014-08-05 14:04:04 +10009690 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9691 return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009692 }
9693
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009694 do {
9695 Py_BEGIN_ALLOW_THREADS
9696 result = writev(fd, iov, cnt);
9697 Py_END_ALLOW_THREADS
9698 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Ross Lagerwall7807c352011-03-17 20:20:30 +02009699
9700 iov_cleanup(iov, buf, cnt);
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009701 if (result < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009702 posix_error();
Victor Stinner57ddf782014-01-08 15:21:28 +01009703
Georg Brandl306336b2012-06-24 12:55:33 +02009704 return result;
Ross Lagerwall7807c352011-03-17 20:20:30 +02009705}
Larry Hastings2f936352014-08-05 14:04:04 +10009706#endif /* HAVE_WRITEV */
9707
9708
9709#ifdef HAVE_PWRITE
9710/*[clinic input]
9711os.pwrite -> Py_ssize_t
9712
9713 fd: int
9714 buffer: Py_buffer
9715 offset: Py_off_t
9716 /
9717
9718Write bytes to a file descriptor starting at a particular offset.
9719
9720Write buffer to fd, starting at offset bytes from the beginning of
9721the file. Returns the number of bytes writte. Does not change the
9722current file offset.
9723[clinic start generated code]*/
9724
Larry Hastings2f936352014-08-05 14:04:04 +10009725static Py_ssize_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009726os_pwrite_impl(PyObject *module, int fd, Py_buffer *buffer, Py_off_t offset)
9727/*[clinic end generated code: output=c74da630758ee925 input=19903f1b3dd26377]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009728{
9729 Py_ssize_t size;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009730 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009731
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009732 do {
9733 Py_BEGIN_ALLOW_THREADS
Steve Dower8fc89802015-04-12 00:26:27 -04009734 _Py_BEGIN_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009735 size = pwrite(fd, buffer->buf, (size_t)buffer->len, offset);
Steve Dower8fc89802015-04-12 00:26:27 -04009736 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009737 Py_END_ALLOW_THREADS
9738 } while (size < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +10009739
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009740 if (size < 0 && !async_err)
Larry Hastings2f936352014-08-05 14:04:04 +10009741 posix_error();
9742 return size;
9743}
9744#endif /* HAVE_PWRITE */
9745
Pablo Galindo4defba32018-01-27 16:16:37 +00009746#if defined(HAVE_PWRITEV) || defined (HAVE_PWRITEV2)
9747/*[clinic input]
9748os.pwritev -> Py_ssize_t
9749
9750 fd: int
9751 buffers: object
9752 offset: Py_off_t
9753 flags: int = 0
9754 /
9755
9756Writes the contents of bytes-like objects to a file descriptor at a given offset.
9757
9758Combines the functionality of writev() and pwrite(). All buffers must be a sequence
9759of bytes-like objects. Buffers are processed in array order. Entire contents of first
9760buffer is written before proceeding to second, and so on. The operating system may
9761set a limit (sysconf() value SC_IOV_MAX) on the number of buffers that can be used.
9762This function writes the contents of each object to the file descriptor and returns
9763the total number of bytes written.
9764
9765The flags argument contains a bitwise OR of zero or more of the following flags:
9766
9767- RWF_DSYNC
9768- RWF_SYNC
9769
9770Using non-zero flags requires Linux 4.7 or newer.
9771[clinic start generated code]*/
9772
9773static Py_ssize_t
9774os_pwritev_impl(PyObject *module, int fd, PyObject *buffers, Py_off_t offset,
9775 int flags)
9776/*[clinic end generated code: output=e3dd3e9d11a6a5c7 input=803dc5ddbf0cfd3b]*/
9777{
9778 Py_ssize_t cnt;
9779 Py_ssize_t result;
9780 int async_err = 0;
9781 struct iovec *iov;
9782 Py_buffer *buf;
9783
9784 if (!PySequence_Check(buffers)) {
9785 PyErr_SetString(PyExc_TypeError,
9786 "pwritev() arg 2 must be a sequence");
9787 return -1;
9788 }
9789
9790 cnt = PySequence_Size(buffers);
9791 if (cnt < 0) {
9792 return -1;
9793 }
9794
9795#ifndef HAVE_PWRITEV2
9796 if(flags != 0) {
9797 argument_unavailable_error("pwritev2", "flags");
9798 return -1;
9799 }
9800#endif
9801
9802 if (iov_setup(&iov, &buf, buffers, cnt, PyBUF_SIMPLE) < 0) {
9803 return -1;
9804 }
9805#ifdef HAVE_PWRITEV2
9806 do {
9807 Py_BEGIN_ALLOW_THREADS
9808 _Py_BEGIN_SUPPRESS_IPH
9809 result = pwritev2(fd, iov, cnt, offset, flags);
9810 _Py_END_SUPPRESS_IPH
9811 Py_END_ALLOW_THREADS
9812 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9813#else
9814 do {
9815 Py_BEGIN_ALLOW_THREADS
9816 _Py_BEGIN_SUPPRESS_IPH
9817 result = pwritev(fd, iov, cnt, offset);
9818 _Py_END_SUPPRESS_IPH
9819 Py_END_ALLOW_THREADS
9820 } while (result < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9821#endif
9822
9823 iov_cleanup(iov, buf, cnt);
9824 if (result < 0) {
9825 if (!async_err) {
9826 posix_error();
9827 }
9828 return -1;
9829 }
9830
9831 return result;
9832}
9833#endif /* HAVE_PWRITEV */
9834
Pablo Galindoaac4d032019-05-31 19:39:47 +01009835#ifdef HAVE_COPY_FILE_RANGE
9836/*[clinic input]
9837
9838os.copy_file_range
9839 src: int
9840 Source file descriptor.
9841 dst: int
9842 Destination file descriptor.
9843 count: Py_ssize_t
9844 Number of bytes to copy.
9845 offset_src: object = None
9846 Starting offset in src.
9847 offset_dst: object = None
9848 Starting offset in dst.
9849
9850Copy count bytes from one file descriptor to another.
9851
9852If offset_src is None, then src is read from the current position;
9853respectively for offset_dst.
9854[clinic start generated code]*/
9855
9856static PyObject *
9857os_copy_file_range_impl(PyObject *module, int src, int dst, Py_ssize_t count,
9858 PyObject *offset_src, PyObject *offset_dst)
9859/*[clinic end generated code: output=1a91713a1d99fc7a input=42fdce72681b25a9]*/
9860{
9861 off_t offset_src_val, offset_dst_val;
9862 off_t *p_offset_src = NULL;
9863 off_t *p_offset_dst = NULL;
9864 Py_ssize_t ret;
9865 int async_err = 0;
9866 /* The flags argument is provided to allow
9867 * for future extensions and currently must be to 0. */
9868 int flags = 0;
Pablo Galindo4defba32018-01-27 16:16:37 +00009869
9870
Pablo Galindoaac4d032019-05-31 19:39:47 +01009871 if (count < 0) {
9872 PyErr_SetString(PyExc_ValueError, "negative value for 'count' not allowed");
9873 return NULL;
9874 }
9875
9876 if (offset_src != Py_None) {
9877 if (!Py_off_t_converter(offset_src, &offset_src_val)) {
9878 return NULL;
9879 }
9880 p_offset_src = &offset_src_val;
9881 }
9882
9883 if (offset_dst != Py_None) {
9884 if (!Py_off_t_converter(offset_dst, &offset_dst_val)) {
9885 return NULL;
9886 }
9887 p_offset_dst = &offset_dst_val;
9888 }
9889
9890 do {
9891 Py_BEGIN_ALLOW_THREADS
9892 ret = copy_file_range(src, p_offset_src, dst, p_offset_dst, count, flags);
9893 Py_END_ALLOW_THREADS
9894 } while (ret < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals()));
9895
9896 if (ret < 0) {
9897 return (!async_err) ? posix_error() : NULL;
9898 }
9899
9900 return PyLong_FromSsize_t(ret);
9901}
9902#endif /* HAVE_COPY_FILE_RANGE*/
Larry Hastings2f936352014-08-05 14:04:04 +10009903
9904#ifdef HAVE_MKFIFO
9905/*[clinic input]
9906os.mkfifo
9907
9908 path: path_t
9909 mode: int=0o666
9910 *
9911 dir_fd: dir_fd(requires='mkfifoat')=None
9912
9913Create a "fifo" (a POSIX named pipe).
9914
9915If dir_fd is not None, it should be a file descriptor open to a directory,
9916 and path should be relative; path will then be relative to that directory.
9917dir_fd may not be implemented on your platform.
9918 If it is unavailable, using it will raise a NotImplementedError.
9919[clinic start generated code]*/
9920
Larry Hastings2f936352014-08-05 14:04:04 +10009921static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009922os_mkfifo_impl(PyObject *module, path_t *path, int mode, int dir_fd)
9923/*[clinic end generated code: output=ce41cfad0e68c940 input=73032e98a36e0e19]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009924{
9925 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009926 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009927
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009928 do {
9929 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009930#ifdef HAVE_MKFIFOAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009931 if (dir_fd != DEFAULT_DIR_FD)
9932 result = mkfifoat(dir_fd, path->narrow, mode);
9933 else
Ross Lagerwall7807c352011-03-17 20:20:30 +02009934#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009935 result = mkfifo(path->narrow, mode);
9936 Py_END_ALLOW_THREADS
9937 } while (result != 0 && errno == EINTR &&
9938 !(async_err = PyErr_CheckSignals()));
9939 if (result != 0)
9940 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009941
9942 Py_RETURN_NONE;
9943}
9944#endif /* HAVE_MKFIFO */
9945
9946
9947#if defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV)
9948/*[clinic input]
9949os.mknod
9950
9951 path: path_t
9952 mode: int=0o600
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +02009953 device: dev_t=0
Larry Hastings2f936352014-08-05 14:04:04 +10009954 *
9955 dir_fd: dir_fd(requires='mknodat')=None
9956
9957Create a node in the file system.
9958
9959Create a node in the file system (file, device special file or named pipe)
9960at path. mode specifies both the permissions to use and the
9961type of node to be created, being combined (bitwise OR) with one of
9962S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. If S_IFCHR or S_IFBLK is set on mode,
9963device defines the newly created device special file (probably using
9964os.makedev()). Otherwise device is ignored.
9965
9966If dir_fd is not None, it should be a file descriptor open to a directory,
9967 and path should be relative; path will then be relative to that directory.
9968dir_fd may not be implemented on your platform.
9969 If it is unavailable, using it will raise a NotImplementedError.
9970[clinic start generated code]*/
9971
Larry Hastings2f936352014-08-05 14:04:04 +10009972static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009973os_mknod_impl(PyObject *module, path_t *path, int mode, dev_t device,
Larry Hastings89964c42015-04-14 18:07:59 -04009974 int dir_fd)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03009975/*[clinic end generated code: output=92e55d3ca8917461 input=ee44531551a4d83b]*/
Larry Hastings2f936352014-08-05 14:04:04 +10009976{
9977 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009978 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +10009979
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009980 do {
9981 Py_BEGIN_ALLOW_THREADS
Larry Hastings2f936352014-08-05 14:04:04 +10009982#ifdef HAVE_MKNODAT
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009983 if (dir_fd != DEFAULT_DIR_FD)
9984 result = mknodat(dir_fd, path->narrow, mode, device);
9985 else
Larry Hastings2f936352014-08-05 14:04:04 +10009986#endif
Charles-François Natali6e6c59b2015-02-07 13:27:50 +00009987 result = mknod(path->narrow, mode, device);
9988 Py_END_ALLOW_THREADS
9989 } while (result != 0 && errno == EINTR &&
9990 !(async_err = PyErr_CheckSignals()));
9991 if (result != 0)
9992 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +10009993
9994 Py_RETURN_NONE;
9995}
9996#endif /* defined(HAVE_MKNOD) && defined(HAVE_MAKEDEV) */
9997
9998
9999#ifdef HAVE_DEVICE_MACROS
10000/*[clinic input]
10001os.major -> unsigned_int
10002
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010003 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010004 /
10005
10006Extracts a device major number from a raw device number.
10007[clinic start generated code]*/
10008
Larry Hastings2f936352014-08-05 14:04:04 +100010009static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010010os_major_impl(PyObject *module, dev_t device)
10011/*[clinic end generated code: output=5b3b2589bafb498e input=1e16a4d30c4d4462]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010012{
10013 return major(device);
10014}
10015
10016
10017/*[clinic input]
10018os.minor -> unsigned_int
10019
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010020 device: dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010021 /
10022
10023Extracts a device minor number from a raw device number.
10024[clinic start generated code]*/
10025
Larry Hastings2f936352014-08-05 14:04:04 +100010026static unsigned int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010027os_minor_impl(PyObject *module, dev_t device)
10028/*[clinic end generated code: output=5e1a25e630b0157d input=0842c6d23f24c65e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010029{
10030 return minor(device);
10031}
10032
10033
10034/*[clinic input]
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010035os.makedev -> dev_t
Larry Hastings2f936352014-08-05 14:04:04 +100010036
10037 major: int
10038 minor: int
10039 /
10040
10041Composes a raw device number from the major and minor device numbers.
10042[clinic start generated code]*/
10043
Serhiy Storchakaacdb7c12015-01-18 11:17:39 +020010044static dev_t
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010045os_makedev_impl(PyObject *module, int major, int minor)
10046/*[clinic end generated code: output=881aaa4aba6f6a52 input=4b9fd8fc73cbe48f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010047{
10048 return makedev(major, minor);
10049}
10050#endif /* HAVE_DEVICE_MACROS */
10051
10052
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010053#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010054/*[clinic input]
10055os.ftruncate
10056
10057 fd: int
10058 length: Py_off_t
10059 /
10060
10061Truncate a file, specified by file descriptor, to a specific length.
10062[clinic start generated code]*/
10063
Larry Hastings2f936352014-08-05 14:04:04 +100010064static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010065os_ftruncate_impl(PyObject *module, int fd, Py_off_t length)
10066/*[clinic end generated code: output=fba15523721be7e4 input=63b43641e52818f2]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010067{
10068 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010069 int async_err = 0;
Larry Hastings2f936352014-08-05 14:04:04 +100010070
Steve Dowerb82e17e2019-05-23 08:45:22 -070010071 if (PySys_Audit("os.truncate", "in", fd, length) < 0) {
10072 return NULL;
10073 }
10074
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010075 do {
10076 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010077 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010078#ifdef MS_WINDOWS
10079 result = _chsize_s(fd, length);
10080#else
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010081 result = ftruncate(fd, length);
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010082#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010083 _Py_END_SUPPRESS_IPH
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010084 Py_END_ALLOW_THREADS
10085 } while (result != 0 && errno == EINTR &&
10086 !(async_err = PyErr_CheckSignals()));
10087 if (result != 0)
10088 return (!async_err) ? posix_error() : NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100010089 Py_RETURN_NONE;
10090}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010091#endif /* HAVE_FTRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010092
10093
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010094#if defined HAVE_TRUNCATE || defined MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010095/*[clinic input]
10096os.truncate
10097 path: path_t(allow_fd='PATH_HAVE_FTRUNCATE')
10098 length: Py_off_t
10099
10100Truncate a file, specified by path, to a specific length.
10101
10102On some platforms, path may also be specified as an open file descriptor.
10103 If this functionality is unavailable, using it raises an exception.
10104[clinic start generated code]*/
10105
Larry Hastings2f936352014-08-05 14:04:04 +100010106static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010107os_truncate_impl(PyObject *module, path_t *path, Py_off_t length)
10108/*[clinic end generated code: output=43009c8df5c0a12b input=77229cf0b50a9b77]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010109{
10110 int result;
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010111#ifdef MS_WINDOWS
10112 int fd;
10113#endif
10114
10115 if (path->fd != -1)
10116 return os_ftruncate_impl(module, path->fd, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010117
Steve Dowerb82e17e2019-05-23 08:45:22 -070010118 if (PySys_Audit("os.truncate", "On", path->object, length) < 0) {
10119 return NULL;
10120 }
10121
Larry Hastings2f936352014-08-05 14:04:04 +100010122 Py_BEGIN_ALLOW_THREADS
Steve Dowera1c7e722015-04-12 00:26:43 -040010123 _Py_BEGIN_SUPPRESS_IPH
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010124#ifdef MS_WINDOWS
Steve Dowercc16be82016-09-08 10:35:16 -070010125 fd = _wopen(path->wide, _O_WRONLY | _O_BINARY | _O_NOINHERIT);
Victor Stinnercc0bbbc2015-04-25 00:21:52 +020010126 if (fd < 0)
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010127 result = -1;
10128 else {
10129 result = _chsize_s(fd, length);
10130 close(fd);
10131 if (result < 0)
10132 errno = result;
10133 }
10134#else
10135 result = truncate(path->narrow, length);
Larry Hastings2f936352014-08-05 14:04:04 +100010136#endif
Steve Dowera1c7e722015-04-12 00:26:43 -040010137 _Py_END_SUPPRESS_IPH
Larry Hastings2f936352014-08-05 14:04:04 +100010138 Py_END_ALLOW_THREADS
10139 if (result < 0)
Alexey Izbyshev83460312018-10-20 03:28:22 +030010140 return posix_path_error(path);
Larry Hastings2f936352014-08-05 14:04:04 +100010141
10142 Py_RETURN_NONE;
10143}
Steve Dowerfe0a41a2015-03-20 19:50:46 -070010144#endif /* HAVE_TRUNCATE || MS_WINDOWS */
Larry Hastings2f936352014-08-05 14:04:04 +100010145
Ross Lagerwall7807c352011-03-17 20:20:30 +020010146
Victor Stinnerd6b17692014-09-30 12:20:05 +020010147/* Issue #22396: On 32-bit AIX platform, the prototypes of os.posix_fadvise()
10148 and os.posix_fallocate() in system headers are wrong if _LARGE_FILES is
10149 defined, which is the case in Python on AIX. AIX bug report:
10150 http://www-01.ibm.com/support/docview.wss?uid=isg1IV56170 */
10151#if defined(_AIX) && defined(_LARGE_FILES) && !defined(__64BIT__)
10152# define POSIX_FADVISE_AIX_BUG
10153#endif
10154
Victor Stinnerec39e262014-09-30 12:35:58 +020010155
Victor Stinnerd6b17692014-09-30 12:20:05 +020010156#if defined(HAVE_POSIX_FALLOCATE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010157/*[clinic input]
10158os.posix_fallocate
10159
10160 fd: int
10161 offset: Py_off_t
10162 length: Py_off_t
10163 /
10164
10165Ensure a file has allocated at least a particular number of bytes on disk.
10166
10167Ensure that the file specified by fd encompasses a range of bytes
10168starting at offset bytes from the beginning and continuing for length bytes.
10169[clinic start generated code]*/
10170
Larry Hastings2f936352014-08-05 14:04:04 +100010171static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010172os_posix_fallocate_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010173 Py_off_t length)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010174/*[clinic end generated code: output=73f107139564aa9d input=d7a2ef0ab2ca52fb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010175{
10176 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010177 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010178
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010179 do {
10180 Py_BEGIN_ALLOW_THREADS
10181 result = posix_fallocate(fd, offset, length);
10182 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010183 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10184
10185 if (result == 0)
10186 Py_RETURN_NONE;
10187
10188 if (async_err)
10189 return NULL;
10190
10191 errno = result;
10192 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010193}
Victor Stinnerec39e262014-09-30 12:35:58 +020010194#endif /* HAVE_POSIX_FALLOCATE) && !POSIX_FADVISE_AIX_BUG */
Larry Hastings2f936352014-08-05 14:04:04 +100010195
Ross Lagerwall7807c352011-03-17 20:20:30 +020010196
Victor Stinnerd6b17692014-09-30 12:20:05 +020010197#if defined(HAVE_POSIX_FADVISE) && !defined(POSIX_FADVISE_AIX_BUG)
Larry Hastings2f936352014-08-05 14:04:04 +100010198/*[clinic input]
10199os.posix_fadvise
10200
10201 fd: int
10202 offset: Py_off_t
10203 length: Py_off_t
10204 advice: int
10205 /
10206
10207Announce an intention to access data in a specific pattern.
10208
10209Announce an intention to access data in a specific pattern, thus allowing
10210the kernel to make optimizations.
10211The advice applies to the region of the file specified by fd starting at
10212offset and continuing for length bytes.
10213advice is one of POSIX_FADV_NORMAL, POSIX_FADV_SEQUENTIAL,
10214POSIX_FADV_RANDOM, POSIX_FADV_NOREUSE, POSIX_FADV_WILLNEED, or
10215POSIX_FADV_DONTNEED.
10216[clinic start generated code]*/
10217
Larry Hastings2f936352014-08-05 14:04:04 +100010218static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010219os_posix_fadvise_impl(PyObject *module, int fd, Py_off_t offset,
Larry Hastings89964c42015-04-14 18:07:59 -040010220 Py_off_t length, int advice)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010221/*[clinic end generated code: output=412ef4aa70c98642 input=0fbe554edc2f04b5]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010222{
10223 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010224 int async_err = 0;
Ross Lagerwall7807c352011-03-17 20:20:30 +020010225
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010226 do {
10227 Py_BEGIN_ALLOW_THREADS
10228 result = posix_fadvise(fd, offset, length, advice);
10229 Py_END_ALLOW_THREADS
Коренберг Маркd4b93e22017-08-14 18:55:16 +050010230 } while (result == EINTR && !(async_err = PyErr_CheckSignals()));
10231
10232 if (result == 0)
10233 Py_RETURN_NONE;
10234
10235 if (async_err)
10236 return NULL;
10237
10238 errno = result;
10239 return posix_error();
Ross Lagerwall7807c352011-03-17 20:20:30 +020010240}
Victor Stinnerec39e262014-09-30 12:35:58 +020010241#endif /* HAVE_POSIX_FADVISE && !POSIX_FADVISE_AIX_BUG */
Ross Lagerwall7807c352011-03-17 20:20:30 +020010242
Guido van Rossumec4f4ac1997-06-02 22:20:51 +000010243
Thomas Hellerf78f12a2007-11-08 19:33:05 +000010244#ifdef MS_WINDOWS
Victor Stinner161e7b32020-01-24 11:53:44 +010010245static PyObject*
10246win32_putenv(PyObject *name, PyObject *value)
10247{
10248 /* Search from index 1 because on Windows starting '=' is allowed for
10249 defining hidden environment variables. */
10250 if (PyUnicode_GET_LENGTH(name) == 0 ||
10251 PyUnicode_FindChar(name, '=', 1, PyUnicode_GET_LENGTH(name), 1) != -1)
10252 {
10253 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10254 return NULL;
10255 }
10256 PyObject *unicode;
10257 if (value != NULL) {
10258 unicode = PyUnicode_FromFormat("%U=%U", name, value);
10259 }
10260 else {
10261 unicode = PyUnicode_FromFormat("%U=", name);
10262 }
10263 if (unicode == NULL) {
10264 return NULL;
10265 }
10266
10267 Py_ssize_t size;
10268 /* PyUnicode_AsWideCharString() rejects embedded null characters */
10269 wchar_t *env = PyUnicode_AsWideCharString(unicode, &size);
10270 Py_DECREF(unicode);
10271
10272 if (env == NULL) {
10273 return NULL;
10274 }
10275 if (size > _MAX_ENV) {
10276 PyErr_Format(PyExc_ValueError,
10277 "the environment variable is longer than %u characters",
10278 _MAX_ENV);
10279 PyMem_Free(env);
10280 return NULL;
10281 }
10282
10283 /* _wputenv() and SetEnvironmentVariableW() update the environment in the
10284 Process Environment Block (PEB). _wputenv() also updates CRT 'environ'
10285 and '_wenviron' variables, whereas SetEnvironmentVariableW() does not.
10286
10287 Prefer _wputenv() to be compatible with C libraries using CRT
10288 variables and CRT functions using these variables (ex: getenv()). */
10289 int err = _wputenv(env);
10290 PyMem_Free(env);
10291
10292 if (err) {
10293 posix_error();
10294 return NULL;
10295 }
10296
10297 Py_RETURN_NONE;
10298}
10299#endif
10300
10301
10302#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010303/*[clinic input]
10304os.putenv
10305
10306 name: unicode
10307 value: unicode
10308 /
10309
10310Change or add an environment variable.
10311[clinic start generated code]*/
10312
Larry Hastings2f936352014-08-05 14:04:04 +100010313static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010314os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10315/*[clinic end generated code: output=d29a567d6b2327d2 input=ba586581c2e6105f]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010316{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010317 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10318 return NULL;
10319 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010320 return win32_putenv(name, value);
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010321}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010322#else
Larry Hastings2f936352014-08-05 14:04:04 +100010323/*[clinic input]
10324os.putenv
Guido van Rossumb6a47161997-09-15 22:54:34 +000010325
Larry Hastings2f936352014-08-05 14:04:04 +100010326 name: FSConverter
10327 value: FSConverter
10328 /
10329
10330Change or add an environment variable.
10331[clinic start generated code]*/
10332
Larry Hastings2f936352014-08-05 14:04:04 +100010333static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010334os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
10335/*[clinic end generated code: output=d29a567d6b2327d2 input=a97bc6152f688d31]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010336{
Serhiy Storchaka77703942017-06-25 07:33:01 +030010337 const char *name_string = PyBytes_AS_STRING(name);
10338 const char *value_string = PyBytes_AS_STRING(value);
Larry Hastings2f936352014-08-05 14:04:04 +100010339
Serhiy Storchaka77703942017-06-25 07:33:01 +030010340 if (strchr(name_string, '=') != NULL) {
10341 PyErr_SetString(PyExc_ValueError, "illegal environment variable name");
10342 return NULL;
10343 }
Victor Stinnerb477d192020-01-22 22:48:16 +010010344
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010345 if (PySys_Audit("os.putenv", "OO", name, value) < 0) {
10346 return NULL;
10347 }
10348
Victor Stinnerb477d192020-01-22 22:48:16 +010010349 if (setenv(name_string, value_string, 1)) {
10350 return posix_error();
10351 }
Larry Hastings2f936352014-08-05 14:04:04 +100010352 Py_RETURN_NONE;
10353}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010354#endif /* !defined(MS_WINDOWS) */
Larry Hastings2f936352014-08-05 14:04:04 +100010355
10356
Victor Stinner161e7b32020-01-24 11:53:44 +010010357#ifdef MS_WINDOWS
10358/*[clinic input]
10359os.unsetenv
10360 name: unicode
10361 /
10362
10363Delete an environment variable.
10364[clinic start generated code]*/
10365
10366static PyObject *
10367os_unsetenv_impl(PyObject *module, PyObject *name)
10368/*[clinic end generated code: output=54c4137ab1834f02 input=4d6a1747cc526d2f]*/
10369{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010370 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10371 return NULL;
10372 }
Victor Stinner161e7b32020-01-24 11:53:44 +010010373 return win32_putenv(name, NULL);
10374}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010375#else
Larry Hastings2f936352014-08-05 14:04:04 +100010376/*[clinic input]
10377os.unsetenv
10378 name: FSConverter
10379 /
10380
10381Delete an environment variable.
10382[clinic start generated code]*/
10383
Larry Hastings2f936352014-08-05 14:04:04 +100010384static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010385os_unsetenv_impl(PyObject *module, PyObject *name)
10386/*[clinic end generated code: output=54c4137ab1834f02 input=2bb5288a599c7107]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010387{
Saiyang Gou7514f4f2020-02-12 23:47:42 -080010388 if (PySys_Audit("os.unsetenv", "(O)", name) < 0) {
10389 return NULL;
10390 }
Victor Stinner984890f2011-11-24 13:53:38 +010010391#ifdef HAVE_BROKEN_UNSETENV
10392 unsetenv(PyBytes_AS_STRING(name));
10393#else
Victor Stinner161e7b32020-01-24 11:53:44 +010010394 int err = unsetenv(PyBytes_AS_STRING(name));
10395 if (err) {
Victor Stinner60b385e2011-11-22 22:01:28 +010010396 return posix_error();
Victor Stinner161e7b32020-01-24 11:53:44 +010010397 }
Victor Stinner984890f2011-11-24 13:53:38 +010010398#endif
Guido van Rossumc524d952001-10-19 01:31:59 +000010399
Victor Stinner84ae1182010-05-06 22:05:07 +000010400 Py_RETURN_NONE;
Guido van Rossumc524d952001-10-19 01:31:59 +000010401}
Victor Stinnerb8d12622020-01-24 14:05:48 +010010402#endif /* !MS_WINDOWS */
Guido van Rossumc524d952001-10-19 01:31:59 +000010403
Larry Hastings2f936352014-08-05 14:04:04 +100010404
10405/*[clinic input]
10406os.strerror
10407
10408 code: int
10409 /
10410
10411Translate an error code to a message string.
10412[clinic start generated code]*/
10413
Larry Hastings2f936352014-08-05 14:04:04 +100010414static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010415os_strerror_impl(PyObject *module, int code)
10416/*[clinic end generated code: output=baebf09fa02a78f2 input=75a8673d97915a91]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010417{
10418 char *message = strerror(code);
Victor Stinner8c62be82010-05-06 00:08:46 +000010419 if (message == NULL) {
10420 PyErr_SetString(PyExc_ValueError,
10421 "strerror() argument out of range");
10422 return NULL;
10423 }
Victor Stinner1b579672011-12-17 05:47:23 +010010424 return PyUnicode_DecodeLocale(message, "surrogateescape");
Guido van Rossumb6a47161997-09-15 22:54:34 +000010425}
Guido van Rossumb6a47161997-09-15 22:54:34 +000010426
Guido van Rossumf1af3fe1996-07-23 19:18:10 +000010427
Guido van Rossumc9641791998-08-04 15:26:23 +000010428#ifdef HAVE_SYS_WAIT_H
Fred Drake106c1a02002-04-23 15:58:02 +000010429#ifdef WCOREDUMP
Larry Hastings2f936352014-08-05 14:04:04 +100010430/*[clinic input]
10431os.WCOREDUMP -> bool
10432
10433 status: int
10434 /
10435
10436Return True if the process returning status was dumped to a core file.
10437[clinic start generated code]*/
10438
Larry Hastings2f936352014-08-05 14:04:04 +100010439static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010440os_WCOREDUMP_impl(PyObject *module, int status)
10441/*[clinic end generated code: output=1a584b147b16bd18 input=8b05e7ab38528d04]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010442{
10443 WAIT_TYPE wait_status;
10444 WAIT_STATUS_INT(wait_status) = status;
10445 return WCOREDUMP(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010446}
10447#endif /* WCOREDUMP */
10448
Larry Hastings2f936352014-08-05 14:04:04 +100010449
Fred Drake106c1a02002-04-23 15:58:02 +000010450#ifdef WIFCONTINUED
Larry Hastings2f936352014-08-05 14:04:04 +100010451/*[clinic input]
10452os.WIFCONTINUED -> bool
10453
10454 status: int
10455
10456Return True if a particular process was continued from a job control stop.
10457
10458Return True if the process returning status was continued from a
10459job control stop.
10460[clinic start generated code]*/
10461
Larry Hastings2f936352014-08-05 14:04:04 +100010462static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010463os_WIFCONTINUED_impl(PyObject *module, int status)
10464/*[clinic end generated code: output=1e35295d844364bd input=e777e7d38eb25bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010465{
10466 WAIT_TYPE wait_status;
10467 WAIT_STATUS_INT(wait_status) = status;
10468 return WIFCONTINUED(wait_status);
Fred Drake106c1a02002-04-23 15:58:02 +000010469}
10470#endif /* WIFCONTINUED */
10471
Larry Hastings2f936352014-08-05 14:04:04 +100010472
Guido van Rossumc9641791998-08-04 15:26:23 +000010473#ifdef WIFSTOPPED
Larry Hastings2f936352014-08-05 14:04:04 +100010474/*[clinic input]
10475os.WIFSTOPPED -> bool
10476
10477 status: int
10478
10479Return True if the process returning status was stopped.
10480[clinic start generated code]*/
10481
Larry Hastings2f936352014-08-05 14:04:04 +100010482static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010483os_WIFSTOPPED_impl(PyObject *module, int status)
10484/*[clinic end generated code: output=fdb57122a5c9b4cb input=043cb7f1289ef904]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010485{
10486 WAIT_TYPE wait_status;
10487 WAIT_STATUS_INT(wait_status) = status;
10488 return WIFSTOPPED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010489}
10490#endif /* WIFSTOPPED */
10491
Larry Hastings2f936352014-08-05 14:04:04 +100010492
Guido van Rossumc9641791998-08-04 15:26:23 +000010493#ifdef WIFSIGNALED
Larry Hastings2f936352014-08-05 14:04:04 +100010494/*[clinic input]
10495os.WIFSIGNALED -> bool
10496
10497 status: int
10498
10499Return True if the process returning status was terminated by a signal.
10500[clinic start generated code]*/
10501
Larry Hastings2f936352014-08-05 14:04:04 +100010502static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010503os_WIFSIGNALED_impl(PyObject *module, int status)
10504/*[clinic end generated code: output=d1dde4dcc819a5f5 input=d55ba7cc9ce5dc43]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010505{
10506 WAIT_TYPE wait_status;
10507 WAIT_STATUS_INT(wait_status) = status;
10508 return WIFSIGNALED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010509}
10510#endif /* WIFSIGNALED */
10511
Larry Hastings2f936352014-08-05 14:04:04 +100010512
Guido van Rossumc9641791998-08-04 15:26:23 +000010513#ifdef WIFEXITED
Larry Hastings2f936352014-08-05 14:04:04 +100010514/*[clinic input]
10515os.WIFEXITED -> bool
10516
10517 status: int
10518
10519Return True if the process returning status exited via the exit() system call.
10520[clinic start generated code]*/
10521
Larry Hastings2f936352014-08-05 14:04:04 +100010522static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010523os_WIFEXITED_impl(PyObject *module, int status)
10524/*[clinic end generated code: output=01c09d6ebfeea397 input=d63775a6791586c0]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010525{
10526 WAIT_TYPE wait_status;
10527 WAIT_STATUS_INT(wait_status) = status;
10528 return WIFEXITED(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010529}
10530#endif /* WIFEXITED */
10531
Larry Hastings2f936352014-08-05 14:04:04 +100010532
Guido van Rossum54ecc3d1999-01-27 17:53:11 +000010533#ifdef WEXITSTATUS
Larry Hastings2f936352014-08-05 14:04:04 +100010534/*[clinic input]
10535os.WEXITSTATUS -> int
10536
10537 status: int
10538
10539Return the process return code from status.
10540[clinic start generated code]*/
10541
Larry Hastings2f936352014-08-05 14:04:04 +100010542static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010543os_WEXITSTATUS_impl(PyObject *module, int status)
10544/*[clinic end generated code: output=6e3efbba11f6488d input=e1fb4944e377585b]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010545{
10546 WAIT_TYPE wait_status;
10547 WAIT_STATUS_INT(wait_status) = status;
10548 return WEXITSTATUS(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010549}
10550#endif /* WEXITSTATUS */
10551
Larry Hastings2f936352014-08-05 14:04:04 +100010552
Guido van Rossumc9641791998-08-04 15:26:23 +000010553#ifdef WTERMSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010554/*[clinic input]
10555os.WTERMSIG -> int
10556
10557 status: int
10558
10559Return the signal that terminated the process that provided the status value.
10560[clinic start generated code]*/
10561
Larry Hastings2f936352014-08-05 14:04:04 +100010562static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010563os_WTERMSIG_impl(PyObject *module, int status)
10564/*[clinic end generated code: output=172f7dfc8dcfc3ad input=727fd7f84ec3f243]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010565{
10566 WAIT_TYPE wait_status;
10567 WAIT_STATUS_INT(wait_status) = status;
10568 return WTERMSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010569}
10570#endif /* WTERMSIG */
10571
Larry Hastings2f936352014-08-05 14:04:04 +100010572
Guido van Rossumc9641791998-08-04 15:26:23 +000010573#ifdef WSTOPSIG
Larry Hastings2f936352014-08-05 14:04:04 +100010574/*[clinic input]
10575os.WSTOPSIG -> int
10576
10577 status: int
10578
10579Return the signal that stopped the process that provided the status value.
10580[clinic start generated code]*/
10581
Larry Hastings2f936352014-08-05 14:04:04 +100010582static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010583os_WSTOPSIG_impl(PyObject *module, int status)
10584/*[clinic end generated code: output=0ab7586396f5d82b input=46ebf1d1b293c5c1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010585{
10586 WAIT_TYPE wait_status;
10587 WAIT_STATUS_INT(wait_status) = status;
10588 return WSTOPSIG(wait_status);
Guido van Rossumc9641791998-08-04 15:26:23 +000010589}
10590#endif /* WSTOPSIG */
Guido van Rossumc9641791998-08-04 15:26:23 +000010591#endif /* HAVE_SYS_WAIT_H */
10592
10593
Thomas Wouters477c8d52006-05-27 19:21:47 +000010594#if defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossumd5753e11999-10-19 13:29:23 +000010595#ifdef _SCO_DS
10596/* SCO OpenServer 5.0 and later requires _SVID3 before it reveals the
10597 needed definitions in sys/statvfs.h */
10598#define _SVID3
10599#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000010600#include <sys/statvfs.h>
10601
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010602static PyObject*
10603_pystatvfs_fromstructstatvfs(struct statvfs st) {
Eddie Elizondob3966632019-11-05 07:16:14 -080010604 PyObject *StatVFSResultType = _posixstate_global->StatVFSResultType;
10605 PyObject *v = PyStructSequence_New((PyTypeObject *)StatVFSResultType);
Victor Stinner8c62be82010-05-06 00:08:46 +000010606 if (v == NULL)
10607 return NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010608
10609#if !defined(HAVE_LARGEFILE_SUPPORT)
Victor Stinner8c62be82010-05-06 00:08:46 +000010610 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10611 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10612 PyStructSequence_SET_ITEM(v, 2, PyLong_FromLong((long) st.f_blocks));
10613 PyStructSequence_SET_ITEM(v, 3, PyLong_FromLong((long) st.f_bfree));
10614 PyStructSequence_SET_ITEM(v, 4, PyLong_FromLong((long) st.f_bavail));
10615 PyStructSequence_SET_ITEM(v, 5, PyLong_FromLong((long) st.f_files));
10616 PyStructSequence_SET_ITEM(v, 6, PyLong_FromLong((long) st.f_ffree));
10617 PyStructSequence_SET_ITEM(v, 7, PyLong_FromLong((long) st.f_favail));
10618 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10619 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010620#else
Victor Stinner8c62be82010-05-06 00:08:46 +000010621 PyStructSequence_SET_ITEM(v, 0, PyLong_FromLong((long) st.f_bsize));
10622 PyStructSequence_SET_ITEM(v, 1, PyLong_FromLong((long) st.f_frsize));
10623 PyStructSequence_SET_ITEM(v, 2,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010624 PyLong_FromLongLong((long long) st.f_blocks));
Victor Stinner8c62be82010-05-06 00:08:46 +000010625 PyStructSequence_SET_ITEM(v, 3,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010626 PyLong_FromLongLong((long long) st.f_bfree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010627 PyStructSequence_SET_ITEM(v, 4,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010628 PyLong_FromLongLong((long long) st.f_bavail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010629 PyStructSequence_SET_ITEM(v, 5,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010630 PyLong_FromLongLong((long long) st.f_files));
Victor Stinner8c62be82010-05-06 00:08:46 +000010631 PyStructSequence_SET_ITEM(v, 6,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010632 PyLong_FromLongLong((long long) st.f_ffree));
Victor Stinner8c62be82010-05-06 00:08:46 +000010633 PyStructSequence_SET_ITEM(v, 7,
Benjamin Petersonaf580df2016-09-06 10:46:49 -070010634 PyLong_FromLongLong((long long) st.f_favail));
Victor Stinner8c62be82010-05-06 00:08:46 +000010635 PyStructSequence_SET_ITEM(v, 8, PyLong_FromLong((long) st.f_flag));
10636 PyStructSequence_SET_ITEM(v, 9, PyLong_FromLong((long) st.f_namemax));
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010637#endif
Michael Felt502d5512018-01-05 13:01:58 +010010638/* The _ALL_SOURCE feature test macro defines f_fsid as a structure
10639 * (issue #32390). */
10640#if defined(_AIX) && defined(_ALL_SOURCE)
10641 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid.val[0]));
10642#else
Giuseppe Scrivano96a5e502017-12-14 23:46:46 +010010643 PyStructSequence_SET_ITEM(v, 10, PyLong_FromUnsignedLong(st.f_fsid));
Michael Felt502d5512018-01-05 13:01:58 +010010644#endif
Victor Stinnerf0a7bac2013-10-30 18:55:24 +010010645 if (PyErr_Occurred()) {
10646 Py_DECREF(v);
10647 return NULL;
10648 }
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010649
Victor Stinner8c62be82010-05-06 00:08:46 +000010650 return v;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010651}
10652
Larry Hastings2f936352014-08-05 14:04:04 +100010653
10654/*[clinic input]
10655os.fstatvfs
10656 fd: int
10657 /
10658
10659Perform an fstatvfs system call on the given fd.
10660
10661Equivalent to statvfs(fd).
10662[clinic start generated code]*/
10663
Larry Hastings2f936352014-08-05 14:04:04 +100010664static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010665os_fstatvfs_impl(PyObject *module, int fd)
10666/*[clinic end generated code: output=53547cf0cc55e6c5 input=d8122243ac50975e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010667{
10668 int result;
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010669 int async_err = 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010670 struct statvfs st;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010671
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010672 do {
10673 Py_BEGIN_ALLOW_THREADS
10674 result = fstatvfs(fd, &st);
10675 Py_END_ALLOW_THREADS
10676 } while (result != 0 && errno == EINTR &&
10677 !(async_err = PyErr_CheckSignals()));
Larry Hastings2f936352014-08-05 14:04:04 +100010678 if (result != 0)
Charles-François Natali6e6c59b2015-02-07 13:27:50 +000010679 return (!async_err) ? posix_error() : NULL;
Guido van Rossum98bf58f2001-10-18 20:34:25 +000010680
Victor Stinner8c62be82010-05-06 00:08:46 +000010681 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010682}
Larry Hastings2f936352014-08-05 14:04:04 +100010683#endif /* defined(HAVE_FSTATVFS) && defined(HAVE_SYS_STATVFS_H) */
Guido van Rossum94f6f721999-01-06 18:42:14 +000010684
10685
Thomas Wouters477c8d52006-05-27 19:21:47 +000010686#if defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H)
Guido van Rossum94f6f721999-01-06 18:42:14 +000010687#include <sys/statvfs.h>
Larry Hastings2f936352014-08-05 14:04:04 +100010688/*[clinic input]
10689os.statvfs
Guido van Rossum94f6f721999-01-06 18:42:14 +000010690
Larry Hastings2f936352014-08-05 14:04:04 +100010691 path: path_t(allow_fd='PATH_HAVE_FSTATVFS')
10692
10693Perform a statvfs system call on the given path.
10694
10695path may always be specified as a string.
10696On some platforms, path may also be specified as an open file descriptor.
10697 If this functionality is unavailable, using it raises an exception.
10698[clinic start generated code]*/
10699
Larry Hastings2f936352014-08-05 14:04:04 +100010700static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010701os_statvfs_impl(PyObject *module, path_t *path)
10702/*[clinic end generated code: output=87106dd1beb8556e input=3f5c35791c669bd9]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010703{
10704 int result;
10705 struct statvfs st;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010706
10707 Py_BEGIN_ALLOW_THREADS
10708#ifdef HAVE_FSTATVFS
Larry Hastings2f936352014-08-05 14:04:04 +100010709 if (path->fd != -1) {
Larry Hastings9cf065c2012-06-22 16:30:09 -070010710#ifdef __APPLE__
10711 /* handle weak-linking on Mac OS X 10.3 */
10712 if (fstatvfs == NULL) {
Larry Hastings2f936352014-08-05 14:04:04 +100010713 fd_specified("statvfs", path->fd);
10714 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070010715 }
10716#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010717 result = fstatvfs(path->fd, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010718 }
10719 else
10720#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010721 result = statvfs(path->narrow, &st);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010722 Py_END_ALLOW_THREADS
10723
10724 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100010725 return path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070010726 }
10727
Larry Hastings2f936352014-08-05 14:04:04 +100010728 return _pystatvfs_fromstructstatvfs(st);
Guido van Rossum94f6f721999-01-06 18:42:14 +000010729}
Larry Hastings2f936352014-08-05 14:04:04 +100010730#endif /* defined(HAVE_STATVFS) && defined(HAVE_SYS_STATVFS_H) */
10731
Guido van Rossum94f6f721999-01-06 18:42:14 +000010732
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010733#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100010734/*[clinic input]
10735os._getdiskusage
10736
Steve Dower23ad6d02018-02-22 10:39:10 -080010737 path: path_t
Larry Hastings2f936352014-08-05 14:04:04 +100010738
10739Return disk usage statistics about the given path as a (total, free) tuple.
10740[clinic start generated code]*/
10741
Larry Hastings2f936352014-08-05 14:04:04 +100010742static PyObject *
Steve Dower23ad6d02018-02-22 10:39:10 -080010743os__getdiskusage_impl(PyObject *module, path_t *path)
10744/*[clinic end generated code: output=3bd3991f5e5c5dfb input=6af8d1b7781cc042]*/
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010745{
10746 BOOL retval;
10747 ULARGE_INTEGER _, total, free;
Joe Pamerc8c02492018-09-25 10:57:36 -040010748 DWORD err = 0;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010749
10750 Py_BEGIN_ALLOW_THREADS
Steve Dower23ad6d02018-02-22 10:39:10 -080010751 retval = GetDiskFreeSpaceExW(path->wide, &_, &total, &free);
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010752 Py_END_ALLOW_THREADS
Joe Pamerc8c02492018-09-25 10:57:36 -040010753 if (retval == 0) {
10754 if (GetLastError() == ERROR_DIRECTORY) {
10755 wchar_t *dir_path = NULL;
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010756
Joe Pamerc8c02492018-09-25 10:57:36 -040010757 dir_path = PyMem_New(wchar_t, path->length + 1);
10758 if (dir_path == NULL) {
10759 return PyErr_NoMemory();
10760 }
10761
10762 wcscpy_s(dir_path, path->length + 1, path->wide);
10763
10764 if (_dirnameW(dir_path) != -1) {
10765 Py_BEGIN_ALLOW_THREADS
10766 retval = GetDiskFreeSpaceExW(dir_path, &_, &total, &free);
10767 Py_END_ALLOW_THREADS
10768 }
10769 /* Record the last error in case it's modified by PyMem_Free. */
10770 err = GetLastError();
10771 PyMem_Free(dir_path);
10772 if (retval) {
10773 goto success;
10774 }
10775 }
10776 return PyErr_SetFromWindowsErr(err);
10777 }
10778
10779success:
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010780 return Py_BuildValue("(LL)", total.QuadPart, free.QuadPart);
10781}
Larry Hastings2f936352014-08-05 14:04:04 +100010782#endif /* MS_WINDOWS */
Giampaolo Rodola'210e7ca2011-07-01 13:55:36 +020010783
10784
Fred Drakec9680921999-12-13 16:37:25 +000010785/* This is used for fpathconf(), pathconf(), confstr() and sysconf().
10786 * It maps strings representing configuration variable names to
10787 * integer values, allowing those functions to be called with the
Thomas Wouters7e474022000-07-16 12:04:32 +000010788 * magic names instead of polluting the module's namespace with tons of
Fred Drake12c6e2d1999-12-14 21:25:03 +000010789 * rarely-used constants. There are three separate tables that use
10790 * these definitions.
Fred Drakebec628d1999-12-15 18:31:10 +000010791 *
10792 * This code is always included, even if none of the interfaces that
10793 * need it are included. The #if hackery needed to avoid it would be
10794 * sufficiently pervasive that it's not worth the loss of readability.
Fred Drakec9680921999-12-13 16:37:25 +000010795 */
10796struct constdef {
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020010797 const char *name;
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010798 int value;
Fred Drakec9680921999-12-13 16:37:25 +000010799};
10800
Fred Drake12c6e2d1999-12-14 21:25:03 +000010801static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010802conv_confname(PyObject *arg, int *valuep, struct constdef *table,
Guido van Rossum7d5baac2007-08-27 23:24:46 +000010803 size_t tablesize)
Fred Drake12c6e2d1999-12-14 21:25:03 +000010804{
Christian Heimes217cfd12007-12-02 14:31:20 +000010805 if (PyLong_Check(arg)) {
Serhiy Storchaka56f6e762015-09-06 21:25:30 +030010806 int value = _PyLong_AsInt(arg);
10807 if (value == -1 && PyErr_Occurred())
10808 return 0;
10809 *valuep = value;
Stefan Krah0e803b32010-11-26 16:16:47 +000010810 return 1;
Fred Drake12c6e2d1999-12-14 21:25:03 +000010811 }
Guido van Rossumbce56a62007-05-10 18:04:33 +000010812 else {
Stefan Krah0e803b32010-11-26 16:16:47 +000010813 /* look up the value in the table using a binary search */
10814 size_t lo = 0;
10815 size_t mid;
10816 size_t hi = tablesize;
10817 int cmp;
10818 const char *confname;
10819 if (!PyUnicode_Check(arg)) {
10820 PyErr_SetString(PyExc_TypeError,
10821 "configuration names must be strings or integers");
10822 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010823 }
Serhiy Storchaka06515832016-11-20 09:13:07 +020010824 confname = PyUnicode_AsUTF8(arg);
Stefan Krah0e803b32010-11-26 16:16:47 +000010825 if (confname == NULL)
10826 return 0;
10827 while (lo < hi) {
10828 mid = (lo + hi) / 2;
10829 cmp = strcmp(confname, table[mid].name);
10830 if (cmp < 0)
10831 hi = mid;
10832 else if (cmp > 0)
10833 lo = mid + 1;
10834 else {
10835 *valuep = table[mid].value;
10836 return 1;
10837 }
10838 }
10839 PyErr_SetString(PyExc_ValueError, "unrecognized configuration name");
10840 return 0;
Victor Stinner8c62be82010-05-06 00:08:46 +000010841 }
Fred Drake12c6e2d1999-12-14 21:25:03 +000010842}
10843
10844
10845#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
10846static struct constdef posix_constants_pathconf[] = {
Fred Draked86ed291999-12-15 15:34:33 +000010847#ifdef _PC_ABI_AIO_XFER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010848 {"PC_ABI_AIO_XFER_MAX", _PC_ABI_AIO_XFER_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000010849#endif
10850#ifdef _PC_ABI_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010851 {"PC_ABI_ASYNC_IO", _PC_ABI_ASYNC_IO},
Fred Draked86ed291999-12-15 15:34:33 +000010852#endif
Fred Drakec9680921999-12-13 16:37:25 +000010853#ifdef _PC_ASYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010854 {"PC_ASYNC_IO", _PC_ASYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010855#endif
10856#ifdef _PC_CHOWN_RESTRICTED
Victor Stinner8c62be82010-05-06 00:08:46 +000010857 {"PC_CHOWN_RESTRICTED", _PC_CHOWN_RESTRICTED},
Fred Drakec9680921999-12-13 16:37:25 +000010858#endif
10859#ifdef _PC_FILESIZEBITS
Victor Stinner8c62be82010-05-06 00:08:46 +000010860 {"PC_FILESIZEBITS", _PC_FILESIZEBITS},
Fred Drakec9680921999-12-13 16:37:25 +000010861#endif
10862#ifdef _PC_LAST
Victor Stinner8c62be82010-05-06 00:08:46 +000010863 {"PC_LAST", _PC_LAST},
Fred Drakec9680921999-12-13 16:37:25 +000010864#endif
10865#ifdef _PC_LINK_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010866 {"PC_LINK_MAX", _PC_LINK_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010867#endif
10868#ifdef _PC_MAX_CANON
Victor Stinner8c62be82010-05-06 00:08:46 +000010869 {"PC_MAX_CANON", _PC_MAX_CANON},
Fred Drakec9680921999-12-13 16:37:25 +000010870#endif
10871#ifdef _PC_MAX_INPUT
Victor Stinner8c62be82010-05-06 00:08:46 +000010872 {"PC_MAX_INPUT", _PC_MAX_INPUT},
Fred Drakec9680921999-12-13 16:37:25 +000010873#endif
10874#ifdef _PC_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010875 {"PC_NAME_MAX", _PC_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010876#endif
10877#ifdef _PC_NO_TRUNC
Victor Stinner8c62be82010-05-06 00:08:46 +000010878 {"PC_NO_TRUNC", _PC_NO_TRUNC},
Fred Drakec9680921999-12-13 16:37:25 +000010879#endif
10880#ifdef _PC_PATH_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000010881 {"PC_PATH_MAX", _PC_PATH_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000010882#endif
10883#ifdef _PC_PIPE_BUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010884 {"PC_PIPE_BUF", _PC_PIPE_BUF},
Fred Drakec9680921999-12-13 16:37:25 +000010885#endif
10886#ifdef _PC_PRIO_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010887 {"PC_PRIO_IO", _PC_PRIO_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010888#endif
10889#ifdef _PC_SOCK_MAXBUF
Victor Stinner8c62be82010-05-06 00:08:46 +000010890 {"PC_SOCK_MAXBUF", _PC_SOCK_MAXBUF},
Fred Drakec9680921999-12-13 16:37:25 +000010891#endif
10892#ifdef _PC_SYNC_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000010893 {"PC_SYNC_IO", _PC_SYNC_IO},
Fred Drakec9680921999-12-13 16:37:25 +000010894#endif
10895#ifdef _PC_VDISABLE
Victor Stinner8c62be82010-05-06 00:08:46 +000010896 {"PC_VDISABLE", _PC_VDISABLE},
Fred Drakec9680921999-12-13 16:37:25 +000010897#endif
Jesus Cea7e9065c2010-10-25 13:02:04 +000010898#ifdef _PC_ACL_ENABLED
10899 {"PC_ACL_ENABLED", _PC_ACL_ENABLED},
10900#endif
10901#ifdef _PC_MIN_HOLE_SIZE
10902 {"PC_MIN_HOLE_SIZE", _PC_MIN_HOLE_SIZE},
10903#endif
10904#ifdef _PC_ALLOC_SIZE_MIN
10905 {"PC_ALLOC_SIZE_MIN", _PC_ALLOC_SIZE_MIN},
10906#endif
10907#ifdef _PC_REC_INCR_XFER_SIZE
10908 {"PC_REC_INCR_XFER_SIZE", _PC_REC_INCR_XFER_SIZE},
10909#endif
10910#ifdef _PC_REC_MAX_XFER_SIZE
10911 {"PC_REC_MAX_XFER_SIZE", _PC_REC_MAX_XFER_SIZE},
10912#endif
10913#ifdef _PC_REC_MIN_XFER_SIZE
10914 {"PC_REC_MIN_XFER_SIZE", _PC_REC_MIN_XFER_SIZE},
10915#endif
10916#ifdef _PC_REC_XFER_ALIGN
10917 {"PC_REC_XFER_ALIGN", _PC_REC_XFER_ALIGN},
10918#endif
10919#ifdef _PC_SYMLINK_MAX
10920 {"PC_SYMLINK_MAX", _PC_SYMLINK_MAX},
10921#endif
10922#ifdef _PC_XATTR_ENABLED
10923 {"PC_XATTR_ENABLED", _PC_XATTR_ENABLED},
10924#endif
10925#ifdef _PC_XATTR_EXISTS
10926 {"PC_XATTR_EXISTS", _PC_XATTR_EXISTS},
10927#endif
10928#ifdef _PC_TIMESTAMP_RESOLUTION
10929 {"PC_TIMESTAMP_RESOLUTION", _PC_TIMESTAMP_RESOLUTION},
10930#endif
Fred Drakec9680921999-12-13 16:37:25 +000010931};
10932
Fred Drakec9680921999-12-13 16:37:25 +000010933static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000010934conv_path_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000010935{
10936 return conv_confname(arg, valuep, posix_constants_pathconf,
10937 sizeof(posix_constants_pathconf)
10938 / sizeof(struct constdef));
10939}
10940#endif
10941
Larry Hastings2f936352014-08-05 14:04:04 +100010942
Fred Drakec9680921999-12-13 16:37:25 +000010943#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010944/*[clinic input]
10945os.fpathconf -> long
10946
10947 fd: int
10948 name: path_confname
10949 /
10950
10951Return the configuration limit name for the file descriptor fd.
10952
10953If there is no limit, return -1.
10954[clinic start generated code]*/
10955
Larry Hastings2f936352014-08-05 14:04:04 +100010956static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010957os_fpathconf_impl(PyObject *module, int fd, int name)
10958/*[clinic end generated code: output=d5b7042425fc3e21 input=5942a024d3777810]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010959{
10960 long limit;
10961
10962 errno = 0;
10963 limit = fpathconf(fd, name);
10964 if (limit == -1 && errno != 0)
10965 posix_error();
10966
10967 return limit;
10968}
10969#endif /* HAVE_FPATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000010970
10971
10972#ifdef HAVE_PATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010973/*[clinic input]
10974os.pathconf -> long
10975 path: path_t(allow_fd='PATH_HAVE_FPATHCONF')
10976 name: path_confname
10977
10978Return the configuration limit name for the file or directory path.
10979
10980If there is no limit, return -1.
10981On some platforms, path may also be specified as an open file descriptor.
10982 If this functionality is unavailable, using it raises an exception.
10983[clinic start generated code]*/
10984
Larry Hastings2f936352014-08-05 14:04:04 +100010985static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030010986os_pathconf_impl(PyObject *module, path_t *path, int name)
10987/*[clinic end generated code: output=5bedee35b293a089 input=bc3e2a985af27e5e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100010988{
Victor Stinner8c62be82010-05-06 00:08:46 +000010989 long limit;
Fred Drakec9680921999-12-13 16:37:25 +000010990
Victor Stinner8c62be82010-05-06 00:08:46 +000010991 errno = 0;
Georg Brandl306336b2012-06-24 12:55:33 +020010992#ifdef HAVE_FPATHCONF
Larry Hastings2f936352014-08-05 14:04:04 +100010993 if (path->fd != -1)
10994 limit = fpathconf(path->fd, name);
Georg Brandl306336b2012-06-24 12:55:33 +020010995 else
10996#endif
Larry Hastings2f936352014-08-05 14:04:04 +100010997 limit = pathconf(path->narrow, name);
Victor Stinner8c62be82010-05-06 00:08:46 +000010998 if (limit == -1 && errno != 0) {
10999 if (errno == EINVAL)
Stefan Krah99439262010-11-26 12:58:05 +000011000 /* could be a path or name problem */
11001 posix_error();
Fred Drakec9680921999-12-13 16:37:25 +000011002 else
Larry Hastings2f936352014-08-05 14:04:04 +100011003 path_error(path);
Victor Stinner8c62be82010-05-06 00:08:46 +000011004 }
Larry Hastings2f936352014-08-05 14:04:04 +100011005
11006 return limit;
Fred Drakec9680921999-12-13 16:37:25 +000011007}
Larry Hastings2f936352014-08-05 14:04:04 +100011008#endif /* HAVE_PATHCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011009
11010#ifdef HAVE_CONFSTR
11011static struct constdef posix_constants_confstr[] = {
Fred Draked86ed291999-12-15 15:34:33 +000011012#ifdef _CS_ARCHITECTURE
Victor Stinner8c62be82010-05-06 00:08:46 +000011013 {"CS_ARCHITECTURE", _CS_ARCHITECTURE},
Fred Draked86ed291999-12-15 15:34:33 +000011014#endif
Mark Dickinson876d7c82010-04-16 12:47:52 +000011015#ifdef _CS_GNU_LIBC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011016 {"CS_GNU_LIBC_VERSION", _CS_GNU_LIBC_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011017#endif
11018#ifdef _CS_GNU_LIBPTHREAD_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011019 {"CS_GNU_LIBPTHREAD_VERSION", _CS_GNU_LIBPTHREAD_VERSION},
Mark Dickinson876d7c82010-04-16 12:47:52 +000011020#endif
Fred Draked86ed291999-12-15 15:34:33 +000011021#ifdef _CS_HOSTNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011022 {"CS_HOSTNAME", _CS_HOSTNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011023#endif
11024#ifdef _CS_HW_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011025 {"CS_HW_PROVIDER", _CS_HW_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011026#endif
11027#ifdef _CS_HW_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011028 {"CS_HW_SERIAL", _CS_HW_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011029#endif
11030#ifdef _CS_INITTAB_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011031 {"CS_INITTAB_NAME", _CS_INITTAB_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011032#endif
Fred Drakec9680921999-12-13 16:37:25 +000011033#ifdef _CS_LFS64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011034 {"CS_LFS64_CFLAGS", _CS_LFS64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011035#endif
11036#ifdef _CS_LFS64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011037 {"CS_LFS64_LDFLAGS", _CS_LFS64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011038#endif
11039#ifdef _CS_LFS64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011040 {"CS_LFS64_LIBS", _CS_LFS64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011041#endif
11042#ifdef _CS_LFS64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011043 {"CS_LFS64_LINTFLAGS", _CS_LFS64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011044#endif
11045#ifdef _CS_LFS_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011046 {"CS_LFS_CFLAGS", _CS_LFS_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011047#endif
11048#ifdef _CS_LFS_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011049 {"CS_LFS_LDFLAGS", _CS_LFS_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011050#endif
11051#ifdef _CS_LFS_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011052 {"CS_LFS_LIBS", _CS_LFS_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011053#endif
11054#ifdef _CS_LFS_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011055 {"CS_LFS_LINTFLAGS", _CS_LFS_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011056#endif
Fred Draked86ed291999-12-15 15:34:33 +000011057#ifdef _CS_MACHINE
Victor Stinner8c62be82010-05-06 00:08:46 +000011058 {"CS_MACHINE", _CS_MACHINE},
Fred Draked86ed291999-12-15 15:34:33 +000011059#endif
Fred Drakec9680921999-12-13 16:37:25 +000011060#ifdef _CS_PATH
Victor Stinner8c62be82010-05-06 00:08:46 +000011061 {"CS_PATH", _CS_PATH},
Fred Drakec9680921999-12-13 16:37:25 +000011062#endif
Fred Draked86ed291999-12-15 15:34:33 +000011063#ifdef _CS_RELEASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011064 {"CS_RELEASE", _CS_RELEASE},
Fred Draked86ed291999-12-15 15:34:33 +000011065#endif
11066#ifdef _CS_SRPC_DOMAIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011067 {"CS_SRPC_DOMAIN", _CS_SRPC_DOMAIN},
Fred Draked86ed291999-12-15 15:34:33 +000011068#endif
11069#ifdef _CS_SYSNAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011070 {"CS_SYSNAME", _CS_SYSNAME},
Fred Draked86ed291999-12-15 15:34:33 +000011071#endif
11072#ifdef _CS_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011073 {"CS_VERSION", _CS_VERSION},
Fred Draked86ed291999-12-15 15:34:33 +000011074#endif
Fred Drakec9680921999-12-13 16:37:25 +000011075#ifdef _CS_XBS5_ILP32_OFF32_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011076 {"CS_XBS5_ILP32_OFF32_CFLAGS", _CS_XBS5_ILP32_OFF32_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011077#endif
11078#ifdef _CS_XBS5_ILP32_OFF32_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011079 {"CS_XBS5_ILP32_OFF32_LDFLAGS", _CS_XBS5_ILP32_OFF32_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011080#endif
11081#ifdef _CS_XBS5_ILP32_OFF32_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011082 {"CS_XBS5_ILP32_OFF32_LIBS", _CS_XBS5_ILP32_OFF32_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011083#endif
11084#ifdef _CS_XBS5_ILP32_OFF32_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011085 {"CS_XBS5_ILP32_OFF32_LINTFLAGS", _CS_XBS5_ILP32_OFF32_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011086#endif
11087#ifdef _CS_XBS5_ILP32_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011088 {"CS_XBS5_ILP32_OFFBIG_CFLAGS", _CS_XBS5_ILP32_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011089#endif
11090#ifdef _CS_XBS5_ILP32_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011091 {"CS_XBS5_ILP32_OFFBIG_LDFLAGS", _CS_XBS5_ILP32_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011092#endif
11093#ifdef _CS_XBS5_ILP32_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011094 {"CS_XBS5_ILP32_OFFBIG_LIBS", _CS_XBS5_ILP32_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011095#endif
11096#ifdef _CS_XBS5_ILP32_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011097 {"CS_XBS5_ILP32_OFFBIG_LINTFLAGS", _CS_XBS5_ILP32_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011098#endif
11099#ifdef _CS_XBS5_LP64_OFF64_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011100 {"CS_XBS5_LP64_OFF64_CFLAGS", _CS_XBS5_LP64_OFF64_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011101#endif
11102#ifdef _CS_XBS5_LP64_OFF64_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011103 {"CS_XBS5_LP64_OFF64_LDFLAGS", _CS_XBS5_LP64_OFF64_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011104#endif
11105#ifdef _CS_XBS5_LP64_OFF64_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011106 {"CS_XBS5_LP64_OFF64_LIBS", _CS_XBS5_LP64_OFF64_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011107#endif
11108#ifdef _CS_XBS5_LP64_OFF64_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011109 {"CS_XBS5_LP64_OFF64_LINTFLAGS", _CS_XBS5_LP64_OFF64_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011110#endif
11111#ifdef _CS_XBS5_LPBIG_OFFBIG_CFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011112 {"CS_XBS5_LPBIG_OFFBIG_CFLAGS", _CS_XBS5_LPBIG_OFFBIG_CFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011113#endif
11114#ifdef _CS_XBS5_LPBIG_OFFBIG_LDFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011115 {"CS_XBS5_LPBIG_OFFBIG_LDFLAGS", _CS_XBS5_LPBIG_OFFBIG_LDFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011116#endif
11117#ifdef _CS_XBS5_LPBIG_OFFBIG_LIBS
Victor Stinner8c62be82010-05-06 00:08:46 +000011118 {"CS_XBS5_LPBIG_OFFBIG_LIBS", _CS_XBS5_LPBIG_OFFBIG_LIBS},
Fred Drakec9680921999-12-13 16:37:25 +000011119#endif
11120#ifdef _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS
Victor Stinner8c62be82010-05-06 00:08:46 +000011121 {"CS_XBS5_LPBIG_OFFBIG_LINTFLAGS", _CS_XBS5_LPBIG_OFFBIG_LINTFLAGS},
Fred Drakec9680921999-12-13 16:37:25 +000011122#endif
Fred Draked86ed291999-12-15 15:34:33 +000011123#ifdef _MIPS_CS_AVAIL_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011124 {"MIPS_CS_AVAIL_PROCESSORS", _MIPS_CS_AVAIL_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011125#endif
11126#ifdef _MIPS_CS_BASE
Victor Stinner8c62be82010-05-06 00:08:46 +000011127 {"MIPS_CS_BASE", _MIPS_CS_BASE},
Fred Draked86ed291999-12-15 15:34:33 +000011128#endif
11129#ifdef _MIPS_CS_HOSTID
Victor Stinner8c62be82010-05-06 00:08:46 +000011130 {"MIPS_CS_HOSTID", _MIPS_CS_HOSTID},
Fred Draked86ed291999-12-15 15:34:33 +000011131#endif
11132#ifdef _MIPS_CS_HW_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011133 {"MIPS_CS_HW_NAME", _MIPS_CS_HW_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011134#endif
11135#ifdef _MIPS_CS_NUM_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011136 {"MIPS_CS_NUM_PROCESSORS", _MIPS_CS_NUM_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011137#endif
11138#ifdef _MIPS_CS_OSREL_MAJ
Victor Stinner8c62be82010-05-06 00:08:46 +000011139 {"MIPS_CS_OSREL_MAJ", _MIPS_CS_OSREL_MAJ},
Fred Draked86ed291999-12-15 15:34:33 +000011140#endif
11141#ifdef _MIPS_CS_OSREL_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011142 {"MIPS_CS_OSREL_MIN", _MIPS_CS_OSREL_MIN},
Fred Draked86ed291999-12-15 15:34:33 +000011143#endif
11144#ifdef _MIPS_CS_OSREL_PATCH
Victor Stinner8c62be82010-05-06 00:08:46 +000011145 {"MIPS_CS_OSREL_PATCH", _MIPS_CS_OSREL_PATCH},
Fred Draked86ed291999-12-15 15:34:33 +000011146#endif
11147#ifdef _MIPS_CS_OS_NAME
Victor Stinner8c62be82010-05-06 00:08:46 +000011148 {"MIPS_CS_OS_NAME", _MIPS_CS_OS_NAME},
Fred Draked86ed291999-12-15 15:34:33 +000011149#endif
11150#ifdef _MIPS_CS_OS_PROVIDER
Victor Stinner8c62be82010-05-06 00:08:46 +000011151 {"MIPS_CS_OS_PROVIDER", _MIPS_CS_OS_PROVIDER},
Fred Draked86ed291999-12-15 15:34:33 +000011152#endif
11153#ifdef _MIPS_CS_PROCESSORS
Victor Stinner8c62be82010-05-06 00:08:46 +000011154 {"MIPS_CS_PROCESSORS", _MIPS_CS_PROCESSORS},
Fred Draked86ed291999-12-15 15:34:33 +000011155#endif
11156#ifdef _MIPS_CS_SERIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000011157 {"MIPS_CS_SERIAL", _MIPS_CS_SERIAL},
Fred Draked86ed291999-12-15 15:34:33 +000011158#endif
11159#ifdef _MIPS_CS_VENDOR
Victor Stinner8c62be82010-05-06 00:08:46 +000011160 {"MIPS_CS_VENDOR", _MIPS_CS_VENDOR},
Fred Draked86ed291999-12-15 15:34:33 +000011161#endif
Fred Drakec9680921999-12-13 16:37:25 +000011162};
11163
11164static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011165conv_confstr_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011166{
11167 return conv_confname(arg, valuep, posix_constants_confstr,
11168 sizeof(posix_constants_confstr)
11169 / sizeof(struct constdef));
11170}
11171
Larry Hastings2f936352014-08-05 14:04:04 +100011172
11173/*[clinic input]
11174os.confstr
11175
11176 name: confstr_confname
11177 /
11178
11179Return a string-valued system configuration variable.
11180[clinic start generated code]*/
11181
Larry Hastings2f936352014-08-05 14:04:04 +100011182static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011183os_confstr_impl(PyObject *module, int name)
11184/*[clinic end generated code: output=bfb0b1b1e49b9383 input=18fb4d0567242e65]*/
Fred Drakec9680921999-12-13 16:37:25 +000011185{
11186 PyObject *result = NULL;
Victor Stinnercb043522010-09-10 23:49:04 +000011187 char buffer[255];
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011188 size_t len;
Fred Drakec9680921999-12-13 16:37:25 +000011189
Victor Stinnercb043522010-09-10 23:49:04 +000011190 errno = 0;
11191 len = confstr(name, buffer, sizeof(buffer));
11192 if (len == 0) {
11193 if (errno) {
11194 posix_error();
11195 return NULL;
Fred Drakec9680921999-12-13 16:37:25 +000011196 }
11197 else {
Victor Stinnercb043522010-09-10 23:49:04 +000011198 Py_RETURN_NONE;
Fred Drakec9680921999-12-13 16:37:25 +000011199 }
11200 }
Victor Stinnercb043522010-09-10 23:49:04 +000011201
Victor Stinnerdd3a6a52013-06-25 23:13:47 +020011202 if (len >= sizeof(buffer)) {
Victor Stinnercbc18f32014-12-05 22:51:51 +010011203 size_t len2;
Victor Stinnercb043522010-09-10 23:49:04 +000011204 char *buf = PyMem_Malloc(len);
11205 if (buf == NULL)
11206 return PyErr_NoMemory();
Victor Stinnercbc18f32014-12-05 22:51:51 +010011207 len2 = confstr(name, buf, len);
11208 assert(len == len2);
Christian Heimes8714cfd2015-04-21 10:57:41 +020011209 result = PyUnicode_DecodeFSDefaultAndSize(buf, len2-1);
Victor Stinnercb043522010-09-10 23:49:04 +000011210 PyMem_Free(buf);
11211 }
11212 else
11213 result = PyUnicode_DecodeFSDefaultAndSize(buffer, len-1);
Fred Drakec9680921999-12-13 16:37:25 +000011214 return result;
11215}
Larry Hastings2f936352014-08-05 14:04:04 +100011216#endif /* HAVE_CONFSTR */
Fred Drakec9680921999-12-13 16:37:25 +000011217
11218
11219#ifdef HAVE_SYSCONF
11220static struct constdef posix_constants_sysconf[] = {
11221#ifdef _SC_2_CHAR_TERM
Victor Stinner8c62be82010-05-06 00:08:46 +000011222 {"SC_2_CHAR_TERM", _SC_2_CHAR_TERM},
Fred Drakec9680921999-12-13 16:37:25 +000011223#endif
11224#ifdef _SC_2_C_BIND
Victor Stinner8c62be82010-05-06 00:08:46 +000011225 {"SC_2_C_BIND", _SC_2_C_BIND},
Fred Drakec9680921999-12-13 16:37:25 +000011226#endif
11227#ifdef _SC_2_C_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011228 {"SC_2_C_DEV", _SC_2_C_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011229#endif
11230#ifdef _SC_2_C_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011231 {"SC_2_C_VERSION", _SC_2_C_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011232#endif
11233#ifdef _SC_2_FORT_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011234 {"SC_2_FORT_DEV", _SC_2_FORT_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011235#endif
11236#ifdef _SC_2_FORT_RUN
Victor Stinner8c62be82010-05-06 00:08:46 +000011237 {"SC_2_FORT_RUN", _SC_2_FORT_RUN},
Fred Drakec9680921999-12-13 16:37:25 +000011238#endif
11239#ifdef _SC_2_LOCALEDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000011240 {"SC_2_LOCALEDEF", _SC_2_LOCALEDEF},
Fred Drakec9680921999-12-13 16:37:25 +000011241#endif
11242#ifdef _SC_2_SW_DEV
Victor Stinner8c62be82010-05-06 00:08:46 +000011243 {"SC_2_SW_DEV", _SC_2_SW_DEV},
Fred Drakec9680921999-12-13 16:37:25 +000011244#endif
11245#ifdef _SC_2_UPE
Victor Stinner8c62be82010-05-06 00:08:46 +000011246 {"SC_2_UPE", _SC_2_UPE},
Fred Drakec9680921999-12-13 16:37:25 +000011247#endif
11248#ifdef _SC_2_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011249 {"SC_2_VERSION", _SC_2_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011250#endif
Fred Draked86ed291999-12-15 15:34:33 +000011251#ifdef _SC_ABI_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011252 {"SC_ABI_ASYNCHRONOUS_IO", _SC_ABI_ASYNCHRONOUS_IO},
Fred Draked86ed291999-12-15 15:34:33 +000011253#endif
11254#ifdef _SC_ACL
Victor Stinner8c62be82010-05-06 00:08:46 +000011255 {"SC_ACL", _SC_ACL},
Fred Draked86ed291999-12-15 15:34:33 +000011256#endif
Fred Drakec9680921999-12-13 16:37:25 +000011257#ifdef _SC_AIO_LISTIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011258 {"SC_AIO_LISTIO_MAX", _SC_AIO_LISTIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011259#endif
Fred Drakec9680921999-12-13 16:37:25 +000011260#ifdef _SC_AIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011261 {"SC_AIO_MAX", _SC_AIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011262#endif
11263#ifdef _SC_AIO_PRIO_DELTA_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011264 {"SC_AIO_PRIO_DELTA_MAX", _SC_AIO_PRIO_DELTA_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011265#endif
11266#ifdef _SC_ARG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011267 {"SC_ARG_MAX", _SC_ARG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011268#endif
11269#ifdef _SC_ASYNCHRONOUS_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011270 {"SC_ASYNCHRONOUS_IO", _SC_ASYNCHRONOUS_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011271#endif
11272#ifdef _SC_ATEXIT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011273 {"SC_ATEXIT_MAX", _SC_ATEXIT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011274#endif
Fred Draked86ed291999-12-15 15:34:33 +000011275#ifdef _SC_AUDIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011276 {"SC_AUDIT", _SC_AUDIT},
Fred Draked86ed291999-12-15 15:34:33 +000011277#endif
Fred Drakec9680921999-12-13 16:37:25 +000011278#ifdef _SC_AVPHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011279 {"SC_AVPHYS_PAGES", _SC_AVPHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011280#endif
11281#ifdef _SC_BC_BASE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011282 {"SC_BC_BASE_MAX", _SC_BC_BASE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011283#endif
11284#ifdef _SC_BC_DIM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011285 {"SC_BC_DIM_MAX", _SC_BC_DIM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011286#endif
11287#ifdef _SC_BC_SCALE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011288 {"SC_BC_SCALE_MAX", _SC_BC_SCALE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011289#endif
11290#ifdef _SC_BC_STRING_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011291 {"SC_BC_STRING_MAX", _SC_BC_STRING_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011292#endif
Fred Draked86ed291999-12-15 15:34:33 +000011293#ifdef _SC_CAP
Victor Stinner8c62be82010-05-06 00:08:46 +000011294 {"SC_CAP", _SC_CAP},
Fred Draked86ed291999-12-15 15:34:33 +000011295#endif
Fred Drakec9680921999-12-13 16:37:25 +000011296#ifdef _SC_CHARCLASS_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011297 {"SC_CHARCLASS_NAME_MAX", _SC_CHARCLASS_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011298#endif
11299#ifdef _SC_CHAR_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011300 {"SC_CHAR_BIT", _SC_CHAR_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011301#endif
11302#ifdef _SC_CHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011303 {"SC_CHAR_MAX", _SC_CHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011304#endif
11305#ifdef _SC_CHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011306 {"SC_CHAR_MIN", _SC_CHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011307#endif
11308#ifdef _SC_CHILD_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011309 {"SC_CHILD_MAX", _SC_CHILD_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011310#endif
11311#ifdef _SC_CLK_TCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011312 {"SC_CLK_TCK", _SC_CLK_TCK},
Fred Drakec9680921999-12-13 16:37:25 +000011313#endif
11314#ifdef _SC_COHER_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011315 {"SC_COHER_BLKSZ", _SC_COHER_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011316#endif
11317#ifdef _SC_COLL_WEIGHTS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011318 {"SC_COLL_WEIGHTS_MAX", _SC_COLL_WEIGHTS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011319#endif
11320#ifdef _SC_DCACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011321 {"SC_DCACHE_ASSOC", _SC_DCACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011322#endif
11323#ifdef _SC_DCACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011324 {"SC_DCACHE_BLKSZ", _SC_DCACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011325#endif
11326#ifdef _SC_DCACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011327 {"SC_DCACHE_LINESZ", _SC_DCACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011328#endif
11329#ifdef _SC_DCACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011330 {"SC_DCACHE_SZ", _SC_DCACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011331#endif
11332#ifdef _SC_DCACHE_TBLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011333 {"SC_DCACHE_TBLKSZ", _SC_DCACHE_TBLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011334#endif
11335#ifdef _SC_DELAYTIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011336 {"SC_DELAYTIMER_MAX", _SC_DELAYTIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011337#endif
11338#ifdef _SC_EQUIV_CLASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011339 {"SC_EQUIV_CLASS_MAX", _SC_EQUIV_CLASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011340#endif
11341#ifdef _SC_EXPR_NEST_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011342 {"SC_EXPR_NEST_MAX", _SC_EXPR_NEST_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011343#endif
11344#ifdef _SC_FSYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000011345 {"SC_FSYNC", _SC_FSYNC},
Fred Drakec9680921999-12-13 16:37:25 +000011346#endif
11347#ifdef _SC_GETGR_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011348 {"SC_GETGR_R_SIZE_MAX", _SC_GETGR_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011349#endif
11350#ifdef _SC_GETPW_R_SIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011351 {"SC_GETPW_R_SIZE_MAX", _SC_GETPW_R_SIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011352#endif
11353#ifdef _SC_ICACHE_ASSOC
Victor Stinner8c62be82010-05-06 00:08:46 +000011354 {"SC_ICACHE_ASSOC", _SC_ICACHE_ASSOC},
Fred Drakec9680921999-12-13 16:37:25 +000011355#endif
11356#ifdef _SC_ICACHE_BLKSZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011357 {"SC_ICACHE_BLKSZ", _SC_ICACHE_BLKSZ},
Fred Drakec9680921999-12-13 16:37:25 +000011358#endif
11359#ifdef _SC_ICACHE_LINESZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011360 {"SC_ICACHE_LINESZ", _SC_ICACHE_LINESZ},
Fred Drakec9680921999-12-13 16:37:25 +000011361#endif
11362#ifdef _SC_ICACHE_SZ
Victor Stinner8c62be82010-05-06 00:08:46 +000011363 {"SC_ICACHE_SZ", _SC_ICACHE_SZ},
Fred Drakec9680921999-12-13 16:37:25 +000011364#endif
Fred Draked86ed291999-12-15 15:34:33 +000011365#ifdef _SC_INF
Victor Stinner8c62be82010-05-06 00:08:46 +000011366 {"SC_INF", _SC_INF},
Fred Draked86ed291999-12-15 15:34:33 +000011367#endif
Fred Drakec9680921999-12-13 16:37:25 +000011368#ifdef _SC_INT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011369 {"SC_INT_MAX", _SC_INT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011370#endif
11371#ifdef _SC_INT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011372 {"SC_INT_MIN", _SC_INT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011373#endif
11374#ifdef _SC_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011375 {"SC_IOV_MAX", _SC_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011376#endif
Fred Draked86ed291999-12-15 15:34:33 +000011377#ifdef _SC_IP_SECOPTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011378 {"SC_IP_SECOPTS", _SC_IP_SECOPTS},
Fred Draked86ed291999-12-15 15:34:33 +000011379#endif
Fred Drakec9680921999-12-13 16:37:25 +000011380#ifdef _SC_JOB_CONTROL
Victor Stinner8c62be82010-05-06 00:08:46 +000011381 {"SC_JOB_CONTROL", _SC_JOB_CONTROL},
Fred Drakec9680921999-12-13 16:37:25 +000011382#endif
Fred Draked86ed291999-12-15 15:34:33 +000011383#ifdef _SC_KERN_POINTERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011384 {"SC_KERN_POINTERS", _SC_KERN_POINTERS},
Fred Draked86ed291999-12-15 15:34:33 +000011385#endif
11386#ifdef _SC_KERN_SIM
Victor Stinner8c62be82010-05-06 00:08:46 +000011387 {"SC_KERN_SIM", _SC_KERN_SIM},
Fred Draked86ed291999-12-15 15:34:33 +000011388#endif
Fred Drakec9680921999-12-13 16:37:25 +000011389#ifdef _SC_LINE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011390 {"SC_LINE_MAX", _SC_LINE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011391#endif
11392#ifdef _SC_LOGIN_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011393 {"SC_LOGIN_NAME_MAX", _SC_LOGIN_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011394#endif
11395#ifdef _SC_LOGNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011396 {"SC_LOGNAME_MAX", _SC_LOGNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011397#endif
11398#ifdef _SC_LONG_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011399 {"SC_LONG_BIT", _SC_LONG_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011400#endif
Fred Draked86ed291999-12-15 15:34:33 +000011401#ifdef _SC_MAC
Victor Stinner8c62be82010-05-06 00:08:46 +000011402 {"SC_MAC", _SC_MAC},
Fred Draked86ed291999-12-15 15:34:33 +000011403#endif
Fred Drakec9680921999-12-13 16:37:25 +000011404#ifdef _SC_MAPPED_FILES
Victor Stinner8c62be82010-05-06 00:08:46 +000011405 {"SC_MAPPED_FILES", _SC_MAPPED_FILES},
Fred Drakec9680921999-12-13 16:37:25 +000011406#endif
11407#ifdef _SC_MAXPID
Victor Stinner8c62be82010-05-06 00:08:46 +000011408 {"SC_MAXPID", _SC_MAXPID},
Fred Drakec9680921999-12-13 16:37:25 +000011409#endif
11410#ifdef _SC_MB_LEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011411 {"SC_MB_LEN_MAX", _SC_MB_LEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011412#endif
11413#ifdef _SC_MEMLOCK
Victor Stinner8c62be82010-05-06 00:08:46 +000011414 {"SC_MEMLOCK", _SC_MEMLOCK},
Fred Drakec9680921999-12-13 16:37:25 +000011415#endif
11416#ifdef _SC_MEMLOCK_RANGE
Victor Stinner8c62be82010-05-06 00:08:46 +000011417 {"SC_MEMLOCK_RANGE", _SC_MEMLOCK_RANGE},
Fred Drakec9680921999-12-13 16:37:25 +000011418#endif
11419#ifdef _SC_MEMORY_PROTECTION
Victor Stinner8c62be82010-05-06 00:08:46 +000011420 {"SC_MEMORY_PROTECTION", _SC_MEMORY_PROTECTION},
Fred Drakec9680921999-12-13 16:37:25 +000011421#endif
11422#ifdef _SC_MESSAGE_PASSING
Victor Stinner8c62be82010-05-06 00:08:46 +000011423 {"SC_MESSAGE_PASSING", _SC_MESSAGE_PASSING},
Fred Drakec9680921999-12-13 16:37:25 +000011424#endif
Fred Draked86ed291999-12-15 15:34:33 +000011425#ifdef _SC_MMAP_FIXED_ALIGNMENT
Victor Stinner8c62be82010-05-06 00:08:46 +000011426 {"SC_MMAP_FIXED_ALIGNMENT", _SC_MMAP_FIXED_ALIGNMENT},
Fred Draked86ed291999-12-15 15:34:33 +000011427#endif
Fred Drakec9680921999-12-13 16:37:25 +000011428#ifdef _SC_MQ_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011429 {"SC_MQ_OPEN_MAX", _SC_MQ_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011430#endif
11431#ifdef _SC_MQ_PRIO_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011432 {"SC_MQ_PRIO_MAX", _SC_MQ_PRIO_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011433#endif
Fred Draked86ed291999-12-15 15:34:33 +000011434#ifdef _SC_NACLS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011435 {"SC_NACLS_MAX", _SC_NACLS_MAX},
Fred Draked86ed291999-12-15 15:34:33 +000011436#endif
Fred Drakec9680921999-12-13 16:37:25 +000011437#ifdef _SC_NGROUPS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011438 {"SC_NGROUPS_MAX", _SC_NGROUPS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011439#endif
11440#ifdef _SC_NL_ARGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011441 {"SC_NL_ARGMAX", _SC_NL_ARGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011442#endif
11443#ifdef _SC_NL_LANGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011444 {"SC_NL_LANGMAX", _SC_NL_LANGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011445#endif
11446#ifdef _SC_NL_MSGMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011447 {"SC_NL_MSGMAX", _SC_NL_MSGMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011448#endif
11449#ifdef _SC_NL_NMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011450 {"SC_NL_NMAX", _SC_NL_NMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011451#endif
11452#ifdef _SC_NL_SETMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011453 {"SC_NL_SETMAX", _SC_NL_SETMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011454#endif
11455#ifdef _SC_NL_TEXTMAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011456 {"SC_NL_TEXTMAX", _SC_NL_TEXTMAX},
Fred Drakec9680921999-12-13 16:37:25 +000011457#endif
11458#ifdef _SC_NPROCESSORS_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011459 {"SC_NPROCESSORS_CONF", _SC_NPROCESSORS_CONF},
Fred Drakec9680921999-12-13 16:37:25 +000011460#endif
11461#ifdef _SC_NPROCESSORS_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011462 {"SC_NPROCESSORS_ONLN", _SC_NPROCESSORS_ONLN},
Fred Drakec9680921999-12-13 16:37:25 +000011463#endif
Fred Draked86ed291999-12-15 15:34:33 +000011464#ifdef _SC_NPROC_CONF
Victor Stinner8c62be82010-05-06 00:08:46 +000011465 {"SC_NPROC_CONF", _SC_NPROC_CONF},
Fred Draked86ed291999-12-15 15:34:33 +000011466#endif
11467#ifdef _SC_NPROC_ONLN
Victor Stinner8c62be82010-05-06 00:08:46 +000011468 {"SC_NPROC_ONLN", _SC_NPROC_ONLN},
Fred Draked86ed291999-12-15 15:34:33 +000011469#endif
Fred Drakec9680921999-12-13 16:37:25 +000011470#ifdef _SC_NZERO
Victor Stinner8c62be82010-05-06 00:08:46 +000011471 {"SC_NZERO", _SC_NZERO},
Fred Drakec9680921999-12-13 16:37:25 +000011472#endif
11473#ifdef _SC_OPEN_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011474 {"SC_OPEN_MAX", _SC_OPEN_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011475#endif
11476#ifdef _SC_PAGESIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011477 {"SC_PAGESIZE", _SC_PAGESIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011478#endif
11479#ifdef _SC_PAGE_SIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011480 {"SC_PAGE_SIZE", _SC_PAGE_SIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011481#endif
Batuhan Taşkaya909f4a32020-04-05 03:40:49 +030011482#ifdef _SC_AIX_REALMEM
11483 {"SC_AIX_REALMEM", _SC_AIX_REALMEM},
11484#endif
Fred Drakec9680921999-12-13 16:37:25 +000011485#ifdef _SC_PASS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011486 {"SC_PASS_MAX", _SC_PASS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011487#endif
11488#ifdef _SC_PHYS_PAGES
Victor Stinner8c62be82010-05-06 00:08:46 +000011489 {"SC_PHYS_PAGES", _SC_PHYS_PAGES},
Fred Drakec9680921999-12-13 16:37:25 +000011490#endif
11491#ifdef _SC_PII
Victor Stinner8c62be82010-05-06 00:08:46 +000011492 {"SC_PII", _SC_PII},
Fred Drakec9680921999-12-13 16:37:25 +000011493#endif
11494#ifdef _SC_PII_INTERNET
Victor Stinner8c62be82010-05-06 00:08:46 +000011495 {"SC_PII_INTERNET", _SC_PII_INTERNET},
Fred Drakec9680921999-12-13 16:37:25 +000011496#endif
11497#ifdef _SC_PII_INTERNET_DGRAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011498 {"SC_PII_INTERNET_DGRAM", _SC_PII_INTERNET_DGRAM},
Fred Drakec9680921999-12-13 16:37:25 +000011499#endif
11500#ifdef _SC_PII_INTERNET_STREAM
Victor Stinner8c62be82010-05-06 00:08:46 +000011501 {"SC_PII_INTERNET_STREAM", _SC_PII_INTERNET_STREAM},
Fred Drakec9680921999-12-13 16:37:25 +000011502#endif
11503#ifdef _SC_PII_OSI
Victor Stinner8c62be82010-05-06 00:08:46 +000011504 {"SC_PII_OSI", _SC_PII_OSI},
Fred Drakec9680921999-12-13 16:37:25 +000011505#endif
11506#ifdef _SC_PII_OSI_CLTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011507 {"SC_PII_OSI_CLTS", _SC_PII_OSI_CLTS},
Fred Drakec9680921999-12-13 16:37:25 +000011508#endif
11509#ifdef _SC_PII_OSI_COTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011510 {"SC_PII_OSI_COTS", _SC_PII_OSI_COTS},
Fred Drakec9680921999-12-13 16:37:25 +000011511#endif
11512#ifdef _SC_PII_OSI_M
Victor Stinner8c62be82010-05-06 00:08:46 +000011513 {"SC_PII_OSI_M", _SC_PII_OSI_M},
Fred Drakec9680921999-12-13 16:37:25 +000011514#endif
11515#ifdef _SC_PII_SOCKET
Victor Stinner8c62be82010-05-06 00:08:46 +000011516 {"SC_PII_SOCKET", _SC_PII_SOCKET},
Fred Drakec9680921999-12-13 16:37:25 +000011517#endif
11518#ifdef _SC_PII_XTI
Victor Stinner8c62be82010-05-06 00:08:46 +000011519 {"SC_PII_XTI", _SC_PII_XTI},
Fred Drakec9680921999-12-13 16:37:25 +000011520#endif
11521#ifdef _SC_POLL
Victor Stinner8c62be82010-05-06 00:08:46 +000011522 {"SC_POLL", _SC_POLL},
Fred Drakec9680921999-12-13 16:37:25 +000011523#endif
11524#ifdef _SC_PRIORITIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011525 {"SC_PRIORITIZED_IO", _SC_PRIORITIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011526#endif
11527#ifdef _SC_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011528 {"SC_PRIORITY_SCHEDULING", _SC_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011529#endif
11530#ifdef _SC_REALTIME_SIGNALS
Victor Stinner8c62be82010-05-06 00:08:46 +000011531 {"SC_REALTIME_SIGNALS", _SC_REALTIME_SIGNALS},
Fred Drakec9680921999-12-13 16:37:25 +000011532#endif
11533#ifdef _SC_RE_DUP_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011534 {"SC_RE_DUP_MAX", _SC_RE_DUP_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011535#endif
11536#ifdef _SC_RTSIG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011537 {"SC_RTSIG_MAX", _SC_RTSIG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011538#endif
11539#ifdef _SC_SAVED_IDS
Victor Stinner8c62be82010-05-06 00:08:46 +000011540 {"SC_SAVED_IDS", _SC_SAVED_IDS},
Fred Drakec9680921999-12-13 16:37:25 +000011541#endif
11542#ifdef _SC_SCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011543 {"SC_SCHAR_MAX", _SC_SCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011544#endif
11545#ifdef _SC_SCHAR_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011546 {"SC_SCHAR_MIN", _SC_SCHAR_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011547#endif
11548#ifdef _SC_SELECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011549 {"SC_SELECT", _SC_SELECT},
Fred Drakec9680921999-12-13 16:37:25 +000011550#endif
11551#ifdef _SC_SEMAPHORES
Victor Stinner8c62be82010-05-06 00:08:46 +000011552 {"SC_SEMAPHORES", _SC_SEMAPHORES},
Fred Drakec9680921999-12-13 16:37:25 +000011553#endif
11554#ifdef _SC_SEM_NSEMS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011555 {"SC_SEM_NSEMS_MAX", _SC_SEM_NSEMS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011556#endif
11557#ifdef _SC_SEM_VALUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011558 {"SC_SEM_VALUE_MAX", _SC_SEM_VALUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011559#endif
11560#ifdef _SC_SHARED_MEMORY_OBJECTS
Victor Stinner8c62be82010-05-06 00:08:46 +000011561 {"SC_SHARED_MEMORY_OBJECTS", _SC_SHARED_MEMORY_OBJECTS},
Fred Drakec9680921999-12-13 16:37:25 +000011562#endif
11563#ifdef _SC_SHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011564 {"SC_SHRT_MAX", _SC_SHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011565#endif
11566#ifdef _SC_SHRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011567 {"SC_SHRT_MIN", _SC_SHRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011568#endif
11569#ifdef _SC_SIGQUEUE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011570 {"SC_SIGQUEUE_MAX", _SC_SIGQUEUE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011571#endif
11572#ifdef _SC_SIGRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011573 {"SC_SIGRT_MAX", _SC_SIGRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011574#endif
11575#ifdef _SC_SIGRT_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011576 {"SC_SIGRT_MIN", _SC_SIGRT_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011577#endif
Fred Draked86ed291999-12-15 15:34:33 +000011578#ifdef _SC_SOFTPOWER
Victor Stinner8c62be82010-05-06 00:08:46 +000011579 {"SC_SOFTPOWER", _SC_SOFTPOWER},
Fred Draked86ed291999-12-15 15:34:33 +000011580#endif
Fred Drakec9680921999-12-13 16:37:25 +000011581#ifdef _SC_SPLIT_CACHE
Victor Stinner8c62be82010-05-06 00:08:46 +000011582 {"SC_SPLIT_CACHE", _SC_SPLIT_CACHE},
Fred Drakec9680921999-12-13 16:37:25 +000011583#endif
11584#ifdef _SC_SSIZE_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011585 {"SC_SSIZE_MAX", _SC_SSIZE_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011586#endif
11587#ifdef _SC_STACK_PROT
Victor Stinner8c62be82010-05-06 00:08:46 +000011588 {"SC_STACK_PROT", _SC_STACK_PROT},
Fred Drakec9680921999-12-13 16:37:25 +000011589#endif
11590#ifdef _SC_STREAM_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011591 {"SC_STREAM_MAX", _SC_STREAM_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011592#endif
11593#ifdef _SC_SYNCHRONIZED_IO
Victor Stinner8c62be82010-05-06 00:08:46 +000011594 {"SC_SYNCHRONIZED_IO", _SC_SYNCHRONIZED_IO},
Fred Drakec9680921999-12-13 16:37:25 +000011595#endif
11596#ifdef _SC_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011597 {"SC_THREADS", _SC_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011598#endif
11599#ifdef _SC_THREAD_ATTR_STACKADDR
Victor Stinner8c62be82010-05-06 00:08:46 +000011600 {"SC_THREAD_ATTR_STACKADDR", _SC_THREAD_ATTR_STACKADDR},
Fred Drakec9680921999-12-13 16:37:25 +000011601#endif
11602#ifdef _SC_THREAD_ATTR_STACKSIZE
Victor Stinner8c62be82010-05-06 00:08:46 +000011603 {"SC_THREAD_ATTR_STACKSIZE", _SC_THREAD_ATTR_STACKSIZE},
Fred Drakec9680921999-12-13 16:37:25 +000011604#endif
11605#ifdef _SC_THREAD_DESTRUCTOR_ITERATIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011606 {"SC_THREAD_DESTRUCTOR_ITERATIONS", _SC_THREAD_DESTRUCTOR_ITERATIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011607#endif
11608#ifdef _SC_THREAD_KEYS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011609 {"SC_THREAD_KEYS_MAX", _SC_THREAD_KEYS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011610#endif
11611#ifdef _SC_THREAD_PRIORITY_SCHEDULING
Victor Stinner8c62be82010-05-06 00:08:46 +000011612 {"SC_THREAD_PRIORITY_SCHEDULING", _SC_THREAD_PRIORITY_SCHEDULING},
Fred Drakec9680921999-12-13 16:37:25 +000011613#endif
11614#ifdef _SC_THREAD_PRIO_INHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011615 {"SC_THREAD_PRIO_INHERIT", _SC_THREAD_PRIO_INHERIT},
Fred Drakec9680921999-12-13 16:37:25 +000011616#endif
11617#ifdef _SC_THREAD_PRIO_PROTECT
Victor Stinner8c62be82010-05-06 00:08:46 +000011618 {"SC_THREAD_PRIO_PROTECT", _SC_THREAD_PRIO_PROTECT},
Fred Drakec9680921999-12-13 16:37:25 +000011619#endif
11620#ifdef _SC_THREAD_PROCESS_SHARED
Victor Stinner8c62be82010-05-06 00:08:46 +000011621 {"SC_THREAD_PROCESS_SHARED", _SC_THREAD_PROCESS_SHARED},
Fred Drakec9680921999-12-13 16:37:25 +000011622#endif
11623#ifdef _SC_THREAD_SAFE_FUNCTIONS
Victor Stinner8c62be82010-05-06 00:08:46 +000011624 {"SC_THREAD_SAFE_FUNCTIONS", _SC_THREAD_SAFE_FUNCTIONS},
Fred Drakec9680921999-12-13 16:37:25 +000011625#endif
11626#ifdef _SC_THREAD_STACK_MIN
Victor Stinner8c62be82010-05-06 00:08:46 +000011627 {"SC_THREAD_STACK_MIN", _SC_THREAD_STACK_MIN},
Fred Drakec9680921999-12-13 16:37:25 +000011628#endif
11629#ifdef _SC_THREAD_THREADS_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011630 {"SC_THREAD_THREADS_MAX", _SC_THREAD_THREADS_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011631#endif
11632#ifdef _SC_TIMERS
Victor Stinner8c62be82010-05-06 00:08:46 +000011633 {"SC_TIMERS", _SC_TIMERS},
Fred Drakec9680921999-12-13 16:37:25 +000011634#endif
11635#ifdef _SC_TIMER_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011636 {"SC_TIMER_MAX", _SC_TIMER_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011637#endif
11638#ifdef _SC_TTY_NAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011639 {"SC_TTY_NAME_MAX", _SC_TTY_NAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011640#endif
11641#ifdef _SC_TZNAME_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011642 {"SC_TZNAME_MAX", _SC_TZNAME_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011643#endif
11644#ifdef _SC_T_IOV_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011645 {"SC_T_IOV_MAX", _SC_T_IOV_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011646#endif
11647#ifdef _SC_UCHAR_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011648 {"SC_UCHAR_MAX", _SC_UCHAR_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011649#endif
11650#ifdef _SC_UINT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011651 {"SC_UINT_MAX", _SC_UINT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011652#endif
11653#ifdef _SC_UIO_MAXIOV
Victor Stinner8c62be82010-05-06 00:08:46 +000011654 {"SC_UIO_MAXIOV", _SC_UIO_MAXIOV},
Fred Drakec9680921999-12-13 16:37:25 +000011655#endif
11656#ifdef _SC_ULONG_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011657 {"SC_ULONG_MAX", _SC_ULONG_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011658#endif
11659#ifdef _SC_USHRT_MAX
Victor Stinner8c62be82010-05-06 00:08:46 +000011660 {"SC_USHRT_MAX", _SC_USHRT_MAX},
Fred Drakec9680921999-12-13 16:37:25 +000011661#endif
11662#ifdef _SC_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011663 {"SC_VERSION", _SC_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011664#endif
11665#ifdef _SC_WORD_BIT
Victor Stinner8c62be82010-05-06 00:08:46 +000011666 {"SC_WORD_BIT", _SC_WORD_BIT},
Fred Drakec9680921999-12-13 16:37:25 +000011667#endif
11668#ifdef _SC_XBS5_ILP32_OFF32
Victor Stinner8c62be82010-05-06 00:08:46 +000011669 {"SC_XBS5_ILP32_OFF32", _SC_XBS5_ILP32_OFF32},
Fred Drakec9680921999-12-13 16:37:25 +000011670#endif
11671#ifdef _SC_XBS5_ILP32_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011672 {"SC_XBS5_ILP32_OFFBIG", _SC_XBS5_ILP32_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011673#endif
11674#ifdef _SC_XBS5_LP64_OFF64
Victor Stinner8c62be82010-05-06 00:08:46 +000011675 {"SC_XBS5_LP64_OFF64", _SC_XBS5_LP64_OFF64},
Fred Drakec9680921999-12-13 16:37:25 +000011676#endif
11677#ifdef _SC_XBS5_LPBIG_OFFBIG
Victor Stinner8c62be82010-05-06 00:08:46 +000011678 {"SC_XBS5_LPBIG_OFFBIG", _SC_XBS5_LPBIG_OFFBIG},
Fred Drakec9680921999-12-13 16:37:25 +000011679#endif
11680#ifdef _SC_XOPEN_CRYPT
Victor Stinner8c62be82010-05-06 00:08:46 +000011681 {"SC_XOPEN_CRYPT", _SC_XOPEN_CRYPT},
Fred Drakec9680921999-12-13 16:37:25 +000011682#endif
11683#ifdef _SC_XOPEN_ENH_I18N
Victor Stinner8c62be82010-05-06 00:08:46 +000011684 {"SC_XOPEN_ENH_I18N", _SC_XOPEN_ENH_I18N},
Fred Drakec9680921999-12-13 16:37:25 +000011685#endif
11686#ifdef _SC_XOPEN_LEGACY
Victor Stinner8c62be82010-05-06 00:08:46 +000011687 {"SC_XOPEN_LEGACY", _SC_XOPEN_LEGACY},
Fred Drakec9680921999-12-13 16:37:25 +000011688#endif
11689#ifdef _SC_XOPEN_REALTIME
Victor Stinner8c62be82010-05-06 00:08:46 +000011690 {"SC_XOPEN_REALTIME", _SC_XOPEN_REALTIME},
Fred Drakec9680921999-12-13 16:37:25 +000011691#endif
11692#ifdef _SC_XOPEN_REALTIME_THREADS
Victor Stinner8c62be82010-05-06 00:08:46 +000011693 {"SC_XOPEN_REALTIME_THREADS", _SC_XOPEN_REALTIME_THREADS},
Fred Drakec9680921999-12-13 16:37:25 +000011694#endif
11695#ifdef _SC_XOPEN_SHM
Victor Stinner8c62be82010-05-06 00:08:46 +000011696 {"SC_XOPEN_SHM", _SC_XOPEN_SHM},
Fred Drakec9680921999-12-13 16:37:25 +000011697#endif
11698#ifdef _SC_XOPEN_UNIX
Victor Stinner8c62be82010-05-06 00:08:46 +000011699 {"SC_XOPEN_UNIX", _SC_XOPEN_UNIX},
Fred Drakec9680921999-12-13 16:37:25 +000011700#endif
11701#ifdef _SC_XOPEN_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011702 {"SC_XOPEN_VERSION", _SC_XOPEN_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011703#endif
11704#ifdef _SC_XOPEN_XCU_VERSION
Victor Stinner8c62be82010-05-06 00:08:46 +000011705 {"SC_XOPEN_XCU_VERSION", _SC_XOPEN_XCU_VERSION},
Fred Drakec9680921999-12-13 16:37:25 +000011706#endif
11707#ifdef _SC_XOPEN_XPG2
Victor Stinner8c62be82010-05-06 00:08:46 +000011708 {"SC_XOPEN_XPG2", _SC_XOPEN_XPG2},
Fred Drakec9680921999-12-13 16:37:25 +000011709#endif
11710#ifdef _SC_XOPEN_XPG3
Victor Stinner8c62be82010-05-06 00:08:46 +000011711 {"SC_XOPEN_XPG3", _SC_XOPEN_XPG3},
Fred Drakec9680921999-12-13 16:37:25 +000011712#endif
11713#ifdef _SC_XOPEN_XPG4
Victor Stinner8c62be82010-05-06 00:08:46 +000011714 {"SC_XOPEN_XPG4", _SC_XOPEN_XPG4},
Fred Drakec9680921999-12-13 16:37:25 +000011715#endif
11716};
11717
11718static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011719conv_sysconf_confname(PyObject *arg, int *valuep)
Fred Drakec9680921999-12-13 16:37:25 +000011720{
11721 return conv_confname(arg, valuep, posix_constants_sysconf,
11722 sizeof(posix_constants_sysconf)
11723 / sizeof(struct constdef));
11724}
11725
Larry Hastings2f936352014-08-05 14:04:04 +100011726
11727/*[clinic input]
11728os.sysconf -> long
11729 name: sysconf_confname
11730 /
11731
11732Return an integer-valued system configuration variable.
11733[clinic start generated code]*/
11734
Larry Hastings2f936352014-08-05 14:04:04 +100011735static long
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011736os_sysconf_impl(PyObject *module, int name)
11737/*[clinic end generated code: output=3662f945fc0cc756 input=279e3430a33f29e4]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011738{
11739 long value;
11740
11741 errno = 0;
11742 value = sysconf(name);
11743 if (value == -1 && errno != 0)
11744 posix_error();
11745 return value;
11746}
11747#endif /* HAVE_SYSCONF */
Fred Drakec9680921999-12-13 16:37:25 +000011748
11749
Fred Drakebec628d1999-12-15 18:31:10 +000011750/* This code is used to ensure that the tables of configuration value names
Serhiy Storchaka56a6d852014-12-01 18:28:43 +020011751 * are in sorted order as required by conv_confname(), and also to build
Fred Drakebec628d1999-12-15 18:31:10 +000011752 * the exported dictionaries that are used to publish information about the
11753 * names available on the host platform.
11754 *
11755 * Sorting the table at runtime ensures that the table is properly ordered
11756 * when used, even for platforms we're not able to test on. It also makes
11757 * it easier to add additional entries to the tables.
Fred Draked86ed291999-12-15 15:34:33 +000011758 */
Fred Drakebec628d1999-12-15 18:31:10 +000011759
11760static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011761cmp_constdefs(const void *v1, const void *v2)
Fred Drakebec628d1999-12-15 18:31:10 +000011762{
11763 const struct constdef *c1 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011764 (const struct constdef *) v1;
Fred Drakebec628d1999-12-15 18:31:10 +000011765 const struct constdef *c2 =
Victor Stinner8c62be82010-05-06 00:08:46 +000011766 (const struct constdef *) v2;
Fred Drakebec628d1999-12-15 18:31:10 +000011767
11768 return strcmp(c1->name, c2->name);
11769}
11770
11771static int
Fredrik Lundhff7df9d2000-07-08 22:48:53 +000011772setup_confname_table(struct constdef *table, size_t tablesize,
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030011773 const char *tablename, PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011774{
Fred Drakebec628d1999-12-15 18:31:10 +000011775 PyObject *d = NULL;
Barry Warsaw3155db32000-04-13 15:20:40 +000011776 size_t i;
Fred Drakebec628d1999-12-15 18:31:10 +000011777
11778 qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
11779 d = PyDict_New();
Barry Warsaw3155db32000-04-13 15:20:40 +000011780 if (d == NULL)
Victor Stinner8c62be82010-05-06 00:08:46 +000011781 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011782
Barry Warsaw3155db32000-04-13 15:20:40 +000011783 for (i=0; i < tablesize; ++i) {
Victor Stinner8c62be82010-05-06 00:08:46 +000011784 PyObject *o = PyLong_FromLong(table[i].value);
11785 if (o == NULL || PyDict_SetItemString(d, table[i].name, o) == -1) {
11786 Py_XDECREF(o);
11787 Py_DECREF(d);
11788 return -1;
11789 }
11790 Py_DECREF(o);
Fred Draked86ed291999-12-15 15:34:33 +000011791 }
Fred Drake4d1e64b2002-04-15 19:40:07 +000011792 return PyModule_AddObject(module, tablename, d);
Fred Draked86ed291999-12-15 15:34:33 +000011793}
11794
Fred Drakebec628d1999-12-15 18:31:10 +000011795/* Return -1 on failure, 0 on success. */
11796static int
Fred Drake4d1e64b2002-04-15 19:40:07 +000011797setup_confname_tables(PyObject *module)
Fred Draked86ed291999-12-15 15:34:33 +000011798{
11799#if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
Fred Drakebec628d1999-12-15 18:31:10 +000011800 if (setup_confname_table(posix_constants_pathconf,
Fred Draked86ed291999-12-15 15:34:33 +000011801 sizeof(posix_constants_pathconf)
11802 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011803 "pathconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011804 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011805#endif
11806#ifdef HAVE_CONFSTR
Fred Drakebec628d1999-12-15 18:31:10 +000011807 if (setup_confname_table(posix_constants_confstr,
Fred Draked86ed291999-12-15 15:34:33 +000011808 sizeof(posix_constants_confstr)
11809 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011810 "confstr_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011811 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011812#endif
11813#ifdef HAVE_SYSCONF
Fred Drakebec628d1999-12-15 18:31:10 +000011814 if (setup_confname_table(posix_constants_sysconf,
Fred Draked86ed291999-12-15 15:34:33 +000011815 sizeof(posix_constants_sysconf)
11816 / sizeof(struct constdef),
Fred Drake4d1e64b2002-04-15 19:40:07 +000011817 "sysconf_names", module))
Stefan Krah0e803b32010-11-26 16:16:47 +000011818 return -1;
Fred Draked86ed291999-12-15 15:34:33 +000011819#endif
Fred Drakebec628d1999-12-15 18:31:10 +000011820 return 0;
Fred Draked86ed291999-12-15 15:34:33 +000011821}
Fred Draked86ed291999-12-15 15:34:33 +000011822
11823
Larry Hastings2f936352014-08-05 14:04:04 +100011824/*[clinic input]
11825os.abort
11826
11827Abort the interpreter immediately.
11828
11829This function 'dumps core' or otherwise fails in the hardest way possible
11830on the hosting operating system. This function never returns.
11831[clinic start generated code]*/
11832
Larry Hastings2f936352014-08-05 14:04:04 +100011833static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011834os_abort_impl(PyObject *module)
11835/*[clinic end generated code: output=dcf52586dad2467c input=cf2c7d98bc504047]*/
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011836{
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011837 abort();
11838 /*NOTREACHED*/
Victor Stinner9a2329f2016-12-05 17:56:36 +010011839#ifndef __clang__
11840 /* Issue #28152: abort() is declared with __attribute__((__noreturn__)).
11841 GCC emits a warning without "return NULL;" (compiler bug?), but Clang
11842 is smarter and emits a warning on the return. */
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011843 Py_FatalError("abort() called from Python code didn't abort!");
11844 return NULL;
Victor Stinner9a2329f2016-12-05 17:56:36 +010011845#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011846}
Fred Drakebec628d1999-12-15 18:31:10 +000011847
Martin v. Löwis6238d2b2002-06-30 15:26:10 +000011848#ifdef MS_WINDOWS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011849/* Grab ShellExecute dynamically from shell32 */
11850static int has_ShellExecute = -1;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011851static HINSTANCE (CALLBACK *Py_ShellExecuteW)(HWND, LPCWSTR, LPCWSTR, LPCWSTR,
11852 LPCWSTR, INT);
11853static int
11854check_ShellExecute()
11855{
11856 HINSTANCE hShell32;
11857
11858 /* only recheck */
11859 if (-1 == has_ShellExecute) {
11860 Py_BEGIN_ALLOW_THREADS
Victor Stinnera9912152017-10-13 13:46:57 -070011861 /* Security note: this call is not vulnerable to "DLL hijacking".
11862 SHELL32 is part of "KnownDLLs" and so Windows always load
11863 the system SHELL32.DLL, even if there is another SHELL32.DLL
11864 in the DLL search path. */
Steve Dower7d0e0c92015-01-24 08:18:24 -080011865 hShell32 = LoadLibraryW(L"SHELL32");
Steve Dower7d0e0c92015-01-24 08:18:24 -080011866 if (hShell32) {
Steve Dower7d0e0c92015-01-24 08:18:24 -080011867 *(FARPROC*)&Py_ShellExecuteW = GetProcAddress(hShell32,
11868 "ShellExecuteW");
Steve Dowercc16be82016-09-08 10:35:16 -070011869 has_ShellExecute = Py_ShellExecuteW != NULL;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011870 } else {
11871 has_ShellExecute = 0;
11872 }
Tony Roberts4860f012019-02-02 18:16:42 +010011873 Py_END_ALLOW_THREADS
Steve Dower7d0e0c92015-01-24 08:18:24 -080011874 }
11875 return has_ShellExecute;
11876}
11877
11878
Steve Dowercc16be82016-09-08 10:35:16 -070011879/*[clinic input]
11880os.startfile
11881 filepath: path_t
11882 operation: Py_UNICODE = NULL
Hirokazu Yamamoto8223c242009-05-17 04:21:53 +000011883
Steve Dowercc16be82016-09-08 10:35:16 -070011884Start a file with its associated application.
11885
11886When "operation" is not specified or "open", this acts like
11887double-clicking the file in Explorer, or giving the file name as an
11888argument to the DOS "start" command: the file is opened with whatever
11889application (if any) its extension is associated.
11890When another "operation" is given, it specifies what should be done with
11891the file. A typical operation is "print".
11892
11893startfile returns as soon as the associated application is launched.
11894There is no option to wait for the application to close, and no way
11895to retrieve the application's exit status.
11896
11897The filepath is relative to the current directory. If you want to use
11898an absolute path, make sure the first character is not a slash ("/");
11899the underlying Win32 ShellExecute function doesn't work if it is.
11900[clinic start generated code]*/
11901
11902static PyObject *
Serhiy Storchakaafb3e712018-12-14 11:19:51 +020011903os_startfile_impl(PyObject *module, path_t *filepath,
11904 const Py_UNICODE *operation)
Serhiy Storchaka279f4462019-09-14 12:24:05 +030011905/*[clinic end generated code: output=66dc311c94d50797 input=c940888a5390f039]*/
Steve Dowercc16be82016-09-08 10:35:16 -070011906{
11907 HINSTANCE rc;
Steve Dower7d0e0c92015-01-24 08:18:24 -080011908
11909 if(!check_ShellExecute()) {
11910 /* If the OS doesn't have ShellExecute, return a
11911 NotImplementedError. */
11912 return PyErr_Format(PyExc_NotImplementedError,
11913 "startfile not available on this platform");
11914 }
11915
Saiyang Gou7514f4f2020-02-12 23:47:42 -080011916 if (PySys_Audit("os.startfile", "Ou", filepath->object, operation) < 0) {
Saiyang Gou95f60012020-02-04 16:15:00 -080011917 return NULL;
11918 }
11919
Victor Stinner8c62be82010-05-06 00:08:46 +000011920 Py_BEGIN_ALLOW_THREADS
Steve Dowercc16be82016-09-08 10:35:16 -070011921 rc = Py_ShellExecuteW((HWND)0, operation, filepath->wide,
Steve Dower7d0e0c92015-01-24 08:18:24 -080011922 NULL, NULL, SW_SHOWNORMAL);
Victor Stinner8c62be82010-05-06 00:08:46 +000011923 Py_END_ALLOW_THREADS
11924
Victor Stinner8c62be82010-05-06 00:08:46 +000011925 if (rc <= (HINSTANCE)32) {
Steve Dowercc16be82016-09-08 10:35:16 -070011926 win32_error_object("startfile", filepath->object);
Victor Stinnereb5657a2011-09-30 01:44:27 +020011927 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000011928 }
Steve Dowercc16be82016-09-08 10:35:16 -070011929 Py_RETURN_NONE;
Tim Petersf58a7aa2000-09-22 10:05:54 +000011930}
Larry Hastings2f936352014-08-05 14:04:04 +100011931#endif /* MS_WINDOWS */
11932
Fred Drake5ab8eaf1999-12-09 21:13:07 +000011933
Martin v. Löwis438b5342002-12-27 10:16:42 +000011934#ifdef HAVE_GETLOADAVG
Larry Hastings2f936352014-08-05 14:04:04 +100011935/*[clinic input]
11936os.getloadavg
11937
11938Return average recent system load information.
11939
11940Return the number of processes in the system run queue averaged over
11941the last 1, 5, and 15 minutes as a tuple of three floats.
11942Raises OSError if the load average was unobtainable.
11943[clinic start generated code]*/
11944
Larry Hastings2f936352014-08-05 14:04:04 +100011945static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011946os_getloadavg_impl(PyObject *module)
11947/*[clinic end generated code: output=9ad3a11bfb4f4bd2 input=3d6d826b76d8a34e]*/
Martin v. Löwis438b5342002-12-27 10:16:42 +000011948{
11949 double loadavg[3];
Martin v. Löwis438b5342002-12-27 10:16:42 +000011950 if (getloadavg(loadavg, 3)!=3) {
Stefan Krah0e803b32010-11-26 16:16:47 +000011951 PyErr_SetString(PyExc_OSError, "Load averages are unobtainable");
11952 return NULL;
Martin v. Löwis438b5342002-12-27 10:16:42 +000011953 } else
Stefan Krah0e803b32010-11-26 16:16:47 +000011954 return Py_BuildValue("ddd", loadavg[0], loadavg[1], loadavg[2]);
Martin v. Löwis438b5342002-12-27 10:16:42 +000011955}
Larry Hastings2f936352014-08-05 14:04:04 +100011956#endif /* HAVE_GETLOADAVG */
Martin v. Löwis438b5342002-12-27 10:16:42 +000011957
Larry Hastings2f936352014-08-05 14:04:04 +100011958
11959/*[clinic input]
11960os.device_encoding
11961 fd: int
11962
11963Return a string describing the encoding of a terminal's file descriptor.
11964
11965The file descriptor must be attached to a terminal.
11966If the device is not a terminal, return None.
11967[clinic start generated code]*/
11968
Larry Hastings2f936352014-08-05 14:04:04 +100011969static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011970os_device_encoding_impl(PyObject *module, int fd)
11971/*[clinic end generated code: output=e0d294bbab7e8c2b input=9e1d4a42b66df312]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011972{
Brett Cannonefb00c02012-02-29 18:31:31 -050011973 return _Py_device_encoding(fd);
Martin v. Löwisd1cd4d42007-08-11 14:02:14 +000011974}
11975
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011976
Larry Hastings2f936352014-08-05 14:04:04 +100011977#ifdef HAVE_SETRESUID
11978/*[clinic input]
11979os.setresuid
11980
11981 ruid: uid_t
11982 euid: uid_t
11983 suid: uid_t
11984 /
11985
11986Set the current process's real, effective, and saved user ids.
11987[clinic start generated code]*/
11988
Larry Hastings2f936352014-08-05 14:04:04 +100011989static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030011990os_setresuid_impl(PyObject *module, uid_t ruid, uid_t euid, uid_t suid)
11991/*[clinic end generated code: output=834a641e15373e97 input=9e33cb79a82792f3]*/
Larry Hastings2f936352014-08-05 14:04:04 +100011992{
Victor Stinner8c62be82010-05-06 00:08:46 +000011993 if (setresuid(ruid, euid, suid) < 0)
11994 return posix_error();
11995 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011996}
Larry Hastings2f936352014-08-05 14:04:04 +100011997#endif /* HAVE_SETRESUID */
11998
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000011999
12000#ifdef HAVE_SETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012001/*[clinic input]
12002os.setresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012003
Larry Hastings2f936352014-08-05 14:04:04 +100012004 rgid: gid_t
12005 egid: gid_t
12006 sgid: gid_t
12007 /
12008
12009Set the current process's real, effective, and saved group ids.
12010[clinic start generated code]*/
12011
Larry Hastings2f936352014-08-05 14:04:04 +100012012static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012013os_setresgid_impl(PyObject *module, gid_t rgid, gid_t egid, gid_t sgid)
12014/*[clinic end generated code: output=6aa402f3d2e514a9 input=33e9e0785ef426b1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012015{
Victor Stinner8c62be82010-05-06 00:08:46 +000012016 if (setresgid(rgid, egid, sgid) < 0)
12017 return posix_error();
12018 Py_RETURN_NONE;
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012019}
Larry Hastings2f936352014-08-05 14:04:04 +100012020#endif /* HAVE_SETRESGID */
12021
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012022
12023#ifdef HAVE_GETRESUID
Larry Hastings2f936352014-08-05 14:04:04 +100012024/*[clinic input]
12025os.getresuid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012026
Larry Hastings2f936352014-08-05 14:04:04 +100012027Return a tuple of the current process's real, effective, and saved user ids.
12028[clinic start generated code]*/
12029
Larry Hastings2f936352014-08-05 14:04:04 +100012030static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012031os_getresuid_impl(PyObject *module)
12032/*[clinic end generated code: output=8e0becff5dece5bf input=41ccfa8e1f6517ad]*/
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012033{
Victor Stinner8c62be82010-05-06 00:08:46 +000012034 uid_t ruid, euid, suid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012035 if (getresuid(&ruid, &euid, &suid) < 0)
12036 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012037 return Py_BuildValue("(NNN)", _PyLong_FromUid(ruid),
12038 _PyLong_FromUid(euid),
12039 _PyLong_FromUid(suid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012040}
Larry Hastings2f936352014-08-05 14:04:04 +100012041#endif /* HAVE_GETRESUID */
12042
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012043
12044#ifdef HAVE_GETRESGID
Larry Hastings2f936352014-08-05 14:04:04 +100012045/*[clinic input]
12046os.getresgid
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012047
Larry Hastings2f936352014-08-05 14:04:04 +100012048Return a tuple of the current process's real, effective, and saved group ids.
12049[clinic start generated code]*/
12050
Larry Hastings2f936352014-08-05 14:04:04 +100012051static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012052os_getresgid_impl(PyObject *module)
12053/*[clinic end generated code: output=2719c4bfcf27fb9f input=517e68db9ca32df6]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012054{
12055 gid_t rgid, egid, sgid;
Victor Stinner8c62be82010-05-06 00:08:46 +000012056 if (getresgid(&rgid, &egid, &sgid) < 0)
12057 return posix_error();
Serhiy Storchaka7cf55992013-02-10 21:56:49 +020012058 return Py_BuildValue("(NNN)", _PyLong_FromGid(rgid),
12059 _PyLong_FromGid(egid),
12060 _PyLong_FromGid(sgid));
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012061}
Larry Hastings2f936352014-08-05 14:04:04 +100012062#endif /* HAVE_GETRESGID */
12063
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000012064
Benjamin Peterson9428d532011-09-14 11:45:52 -040012065#ifdef USE_XATTRS
Larry Hastings2f936352014-08-05 14:04:04 +100012066/*[clinic input]
12067os.getxattr
Benjamin Peterson799bd802011-08-31 22:15:17 -040012068
Larry Hastings2f936352014-08-05 14:04:04 +100012069 path: path_t(allow_fd=True)
12070 attribute: path_t
12071 *
12072 follow_symlinks: bool = True
12073
12074Return the value of extended attribute attribute on path.
12075
BNMetricsb9427072018-11-02 15:20:19 +000012076path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012077If follow_symlinks is False, and the last element of the path is a symbolic
12078 link, getxattr will examine the symbolic link itself instead of the file
12079 the link points to.
12080
12081[clinic start generated code]*/
12082
Larry Hastings2f936352014-08-05 14:04:04 +100012083static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012084os_getxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012085 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012086/*[clinic end generated code: output=5f2f44200a43cff2 input=025789491708f7eb]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012087{
12088 Py_ssize_t i;
12089 PyObject *buffer = NULL;
12090
12091 if (fd_and_follow_symlinks_invalid("getxattr", path->fd, follow_symlinks))
12092 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012093
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012094 if (PySys_Audit("os.getxattr", "OO", path->object, attribute->object) < 0) {
12095 return NULL;
12096 }
12097
Larry Hastings9cf065c2012-06-22 16:30:09 -070012098 for (i = 0; ; i++) {
12099 void *ptr;
12100 ssize_t result;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012101 static const Py_ssize_t buffer_sizes[] = {128, XATTR_SIZE_MAX, 0};
Larry Hastings9cf065c2012-06-22 16:30:09 -070012102 Py_ssize_t buffer_size = buffer_sizes[i];
12103 if (!buffer_size) {
Larry Hastings2f936352014-08-05 14:04:04 +100012104 path_error(path);
12105 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012106 }
12107 buffer = PyBytes_FromStringAndSize(NULL, buffer_size);
12108 if (!buffer)
Larry Hastings2f936352014-08-05 14:04:04 +100012109 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012110 ptr = PyBytes_AS_STRING(buffer);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012111
Larry Hastings9cf065c2012-06-22 16:30:09 -070012112 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012113 if (path->fd >= 0)
12114 result = fgetxattr(path->fd, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012115 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012116 result = getxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012117 else
Larry Hastings2f936352014-08-05 14:04:04 +100012118 result = lgetxattr(path->narrow, attribute->narrow, ptr, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012119 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012120
Larry Hastings9cf065c2012-06-22 16:30:09 -070012121 if (result < 0) {
12122 Py_DECREF(buffer);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012123 if (errno == ERANGE)
12124 continue;
Larry Hastings2f936352014-08-05 14:04:04 +100012125 path_error(path);
12126 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012127 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012128
Larry Hastings9cf065c2012-06-22 16:30:09 -070012129 if (result != buffer_size) {
12130 /* Can only shrink. */
12131 _PyBytes_Resize(&buffer, result);
12132 }
12133 break;
12134 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012135
Larry Hastings9cf065c2012-06-22 16:30:09 -070012136 return buffer;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012137}
12138
Larry Hastings2f936352014-08-05 14:04:04 +100012139
12140/*[clinic input]
12141os.setxattr
12142
12143 path: path_t(allow_fd=True)
12144 attribute: path_t
12145 value: Py_buffer
12146 flags: int = 0
12147 *
12148 follow_symlinks: bool = True
12149
12150Set extended attribute attribute on path to value.
12151
BNMetricsb9427072018-11-02 15:20:19 +000012152path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012153If follow_symlinks is False, and the last element of the path is a symbolic
12154 link, setxattr will modify the symbolic link itself instead of the file
12155 the link points to.
12156
12157[clinic start generated code]*/
12158
Benjamin Peterson799bd802011-08-31 22:15:17 -040012159static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012160os_setxattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012161 Py_buffer *value, int flags, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012162/*[clinic end generated code: output=98b83f63fdde26bb input=c17c0103009042f0]*/
Benjamin Peterson799bd802011-08-31 22:15:17 -040012163{
Larry Hastings2f936352014-08-05 14:04:04 +100012164 ssize_t result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012165
Larry Hastings2f936352014-08-05 14:04:04 +100012166 if (fd_and_follow_symlinks_invalid("setxattr", path->fd, follow_symlinks))
Benjamin Peterson799bd802011-08-31 22:15:17 -040012167 return NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012168
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012169 if (PySys_Audit("os.setxattr", "OOy#i", path->object, attribute->object,
12170 value->buf, value->len, flags) < 0) {
12171 return NULL;
12172 }
12173
Benjamin Peterson799bd802011-08-31 22:15:17 -040012174 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012175 if (path->fd > -1)
12176 result = fsetxattr(path->fd, attribute->narrow,
12177 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012178 else if (follow_symlinks)
Larry Hastings2f936352014-08-05 14:04:04 +100012179 result = setxattr(path->narrow, attribute->narrow,
12180 value->buf, value->len, flags);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012181 else
Larry Hastings2f936352014-08-05 14:04:04 +100012182 result = lsetxattr(path->narrow, attribute->narrow,
12183 value->buf, value->len, flags);
Benjamin Peterson799bd802011-08-31 22:15:17 -040012184 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012185
Larry Hastings9cf065c2012-06-22 16:30:09 -070012186 if (result) {
Larry Hastings2f936352014-08-05 14:04:04 +100012187 path_error(path);
12188 return NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012189 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012190
Larry Hastings2f936352014-08-05 14:04:04 +100012191 Py_RETURN_NONE;
12192}
12193
12194
12195/*[clinic input]
12196os.removexattr
12197
12198 path: path_t(allow_fd=True)
12199 attribute: path_t
12200 *
12201 follow_symlinks: bool = True
12202
12203Remove extended attribute attribute on path.
12204
BNMetricsb9427072018-11-02 15:20:19 +000012205path may be either a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012206If follow_symlinks is False, and the last element of the path is a symbolic
12207 link, removexattr will modify the symbolic link itself instead of the file
12208 the link points to.
12209
12210[clinic start generated code]*/
12211
Larry Hastings2f936352014-08-05 14:04:04 +100012212static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012213os_removexattr_impl(PyObject *module, path_t *path, path_t *attribute,
Larry Hastings89964c42015-04-14 18:07:59 -040012214 int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012215/*[clinic end generated code: output=521a51817980cda6 input=3d9a7d36fe2f7c4e]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012216{
12217 ssize_t result;
12218
12219 if (fd_and_follow_symlinks_invalid("removexattr", path->fd, follow_symlinks))
12220 return NULL;
12221
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012222 if (PySys_Audit("os.removexattr", "OO", path->object, attribute->object) < 0) {
12223 return NULL;
12224 }
12225
Larry Hastings2f936352014-08-05 14:04:04 +100012226 Py_BEGIN_ALLOW_THREADS;
12227 if (path->fd > -1)
12228 result = fremovexattr(path->fd, attribute->narrow);
12229 else if (follow_symlinks)
12230 result = removexattr(path->narrow, attribute->narrow);
12231 else
12232 result = lremovexattr(path->narrow, attribute->narrow);
12233 Py_END_ALLOW_THREADS;
12234
12235 if (result) {
12236 return path_error(path);
12237 }
12238
12239 Py_RETURN_NONE;
12240}
12241
12242
12243/*[clinic input]
12244os.listxattr
12245
12246 path: path_t(allow_fd=True, nullable=True) = None
12247 *
12248 follow_symlinks: bool = True
12249
12250Return a list of extended attributes on path.
12251
BNMetricsb9427072018-11-02 15:20:19 +000012252path may be either None, a string, a path-like object, or an open file descriptor.
Larry Hastings2f936352014-08-05 14:04:04 +100012253if path is None, listxattr will examine the current directory.
12254If follow_symlinks is False, and the last element of the path is a symbolic
12255 link, listxattr will examine the symbolic link itself instead of the file
12256 the link points to.
12257[clinic start generated code]*/
12258
Larry Hastings2f936352014-08-05 14:04:04 +100012259static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012260os_listxattr_impl(PyObject *module, path_t *path, int follow_symlinks)
BNMetricsb9427072018-11-02 15:20:19 +000012261/*[clinic end generated code: output=bebdb4e2ad0ce435 input=9826edf9fdb90869]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012262{
Larry Hastings9cf065c2012-06-22 16:30:09 -070012263 Py_ssize_t i;
12264 PyObject *result = NULL;
Larry Hastings2f936352014-08-05 14:04:04 +100012265 const char *name;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012266 char *buffer = NULL;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012267
Larry Hastings2f936352014-08-05 14:04:04 +100012268 if (fd_and_follow_symlinks_invalid("listxattr", path->fd, follow_symlinks))
Larry Hastings9cf065c2012-06-22 16:30:09 -070012269 goto exit;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012270
Saiyang Gou7514f4f2020-02-12 23:47:42 -080012271 if (PySys_Audit("os.listxattr", "(O)",
12272 path->object ? path->object : Py_None) < 0) {
12273 return NULL;
12274 }
12275
Larry Hastings2f936352014-08-05 14:04:04 +100012276 name = path->narrow ? path->narrow : ".";
12277
Larry Hastings9cf065c2012-06-22 16:30:09 -070012278 for (i = 0; ; i++) {
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012279 const char *start, *trace, *end;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012280 ssize_t length;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020012281 static const Py_ssize_t buffer_sizes[] = { 256, XATTR_LIST_MAX, 0 };
Larry Hastings9cf065c2012-06-22 16:30:09 -070012282 Py_ssize_t buffer_size = buffer_sizes[i];
12283 if (!buffer_size) {
Christian Heimes3b9493b2012-09-23 16:11:15 +020012284 /* ERANGE */
Larry Hastings2f936352014-08-05 14:04:04 +100012285 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012286 break;
12287 }
12288 buffer = PyMem_MALLOC(buffer_size);
12289 if (!buffer) {
12290 PyErr_NoMemory();
12291 break;
12292 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012293
Larry Hastings9cf065c2012-06-22 16:30:09 -070012294 Py_BEGIN_ALLOW_THREADS;
Larry Hastings2f936352014-08-05 14:04:04 +100012295 if (path->fd > -1)
12296 length = flistxattr(path->fd, buffer, buffer_size);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012297 else if (follow_symlinks)
12298 length = listxattr(name, buffer, buffer_size);
12299 else
12300 length = llistxattr(name, buffer, buffer_size);
12301 Py_END_ALLOW_THREADS;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012302
Larry Hastings9cf065c2012-06-22 16:30:09 -070012303 if (length < 0) {
Antoine Pitrou7f987392013-05-13 19:46:29 +020012304 if (errno == ERANGE) {
12305 PyMem_FREE(buffer);
Benjamin Petersondedac522013-05-13 19:55:40 -050012306 buffer = NULL;
Larry Hastings9cf065c2012-06-22 16:30:09 -070012307 continue;
Antoine Pitrou7f987392013-05-13 19:46:29 +020012308 }
Larry Hastings2f936352014-08-05 14:04:04 +100012309 path_error(path);
Larry Hastings9cf065c2012-06-22 16:30:09 -070012310 break;
12311 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012312
Larry Hastings9cf065c2012-06-22 16:30:09 -070012313 result = PyList_New(0);
12314 if (!result) {
12315 goto exit;
12316 }
Benjamin Peterson799bd802011-08-31 22:15:17 -040012317
Larry Hastings9cf065c2012-06-22 16:30:09 -070012318 end = buffer + length;
12319 for (trace = start = buffer; trace != end; trace++) {
12320 if (!*trace) {
12321 int error;
12322 PyObject *attribute = PyUnicode_DecodeFSDefaultAndSize(start,
12323 trace - start);
12324 if (!attribute) {
12325 Py_DECREF(result);
12326 result = NULL;
12327 goto exit;
12328 }
12329 error = PyList_Append(result, attribute);
12330 Py_DECREF(attribute);
12331 if (error) {
12332 Py_DECREF(result);
12333 result = NULL;
12334 goto exit;
12335 }
12336 start = trace + 1;
12337 }
12338 }
12339 break;
12340 }
12341exit:
Larry Hastings9cf065c2012-06-22 16:30:09 -070012342 if (buffer)
12343 PyMem_FREE(buffer);
12344 return result;
Benjamin Peterson799bd802011-08-31 22:15:17 -040012345}
Benjamin Peterson9428d532011-09-14 11:45:52 -040012346#endif /* USE_XATTRS */
Benjamin Peterson799bd802011-08-31 22:15:17 -040012347
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012348
Larry Hastings2f936352014-08-05 14:04:04 +100012349/*[clinic input]
12350os.urandom
12351
12352 size: Py_ssize_t
12353 /
12354
12355Return a bytes object containing random bytes suitable for cryptographic use.
12356[clinic start generated code]*/
12357
Larry Hastings2f936352014-08-05 14:04:04 +100012358static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012359os_urandom_impl(PyObject *module, Py_ssize_t size)
12360/*[clinic end generated code: output=42c5cca9d18068e9 input=4067cdb1b6776c29]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012361{
12362 PyObject *bytes;
12363 int result;
12364
Georg Brandl2fb477c2012-02-21 00:33:36 +010012365 if (size < 0)
12366 return PyErr_Format(PyExc_ValueError,
12367 "negative argument not allowed");
Larry Hastings2f936352014-08-05 14:04:04 +100012368 bytes = PyBytes_FromStringAndSize(NULL, size);
12369 if (bytes == NULL)
Georg Brandl2fb477c2012-02-21 00:33:36 +010012370 return NULL;
12371
Victor Stinnere66987e2016-09-06 16:33:52 -070012372 result = _PyOS_URandom(PyBytes_AS_STRING(bytes), PyBytes_GET_SIZE(bytes));
Larry Hastings2f936352014-08-05 14:04:04 +100012373 if (result == -1) {
12374 Py_DECREF(bytes);
Georg Brandl2fb477c2012-02-21 00:33:36 +010012375 return NULL;
12376 }
Larry Hastings2f936352014-08-05 14:04:04 +100012377 return bytes;
Georg Brandl2fb477c2012-02-21 00:33:36 +010012378}
12379
Zackery Spytz43fdbd22019-05-29 13:57:07 -060012380#ifdef HAVE_MEMFD_CREATE
12381/*[clinic input]
12382os.memfd_create
12383
12384 name: FSConverter
12385 flags: unsigned_int(bitwise=True, c_default="MFD_CLOEXEC") = MFD_CLOEXEC
12386
12387[clinic start generated code]*/
12388
12389static PyObject *
12390os_memfd_create_impl(PyObject *module, PyObject *name, unsigned int flags)
12391/*[clinic end generated code: output=6681ede983bdb9a6 input=a42cfc199bcd56e9]*/
12392{
12393 int fd;
12394 const char *bytes = PyBytes_AS_STRING(name);
12395 Py_BEGIN_ALLOW_THREADS
12396 fd = memfd_create(bytes, flags);
12397 Py_END_ALLOW_THREADS
12398 if (fd == -1) {
12399 return PyErr_SetFromErrno(PyExc_OSError);
12400 }
12401 return PyLong_FromLong(fd);
12402}
12403#endif
12404
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012405/* Terminal size querying */
12406
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012407PyDoc_STRVAR(TerminalSize_docstring,
12408 "A tuple of (columns, lines) for holding terminal window size");
12409
12410static PyStructSequence_Field TerminalSize_fields[] = {
12411 {"columns", "width of the terminal window in characters"},
12412 {"lines", "height of the terminal window in characters"},
12413 {NULL, NULL}
12414};
12415
12416static PyStructSequence_Desc TerminalSize_desc = {
12417 "os.terminal_size",
12418 TerminalSize_docstring,
12419 TerminalSize_fields,
12420 2,
12421};
12422
12423#if defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL)
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012424/*[clinic input]
12425os.get_terminal_size
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012426
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012427 fd: int(c_default="fileno(stdout)", py_default="<unrepresentable>") = -1
12428 /
12429
12430Return the size of the terminal window as (columns, lines).
12431
12432The optional argument fd (default standard output) specifies
12433which file descriptor should be queried.
12434
12435If the file descriptor is not connected to a terminal, an OSError
12436is thrown.
12437
12438This function will only be defined if an implementation is
12439available for this system.
12440
12441shutil.get_terminal_size is the high-level function which should
12442normally be used, os.get_terminal_size is the low-level implementation.
12443[clinic start generated code]*/
12444
12445static PyObject *
12446os_get_terminal_size_impl(PyObject *module, int fd)
12447/*[clinic end generated code: output=fbab93acef980508 input=ead5679b82ddb920]*/
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012448{
12449 int columns, lines;
12450 PyObject *termsize;
12451
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012452 /* Under some conditions stdout may not be connected and
12453 * fileno(stdout) may point to an invalid file descriptor. For example
12454 * GUI apps don't have valid standard streams by default.
12455 *
12456 * If this happens, and the optional fd argument is not present,
12457 * the ioctl below will fail returning EBADF. This is what we want.
12458 */
12459
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012460#ifdef TERMSIZE_USE_IOCTL
12461 {
12462 struct winsize w;
12463 if (ioctl(fd, TIOCGWINSZ, &w))
12464 return PyErr_SetFromErrno(PyExc_OSError);
12465 columns = w.ws_col;
12466 lines = w.ws_row;
12467 }
12468#endif /* TERMSIZE_USE_IOCTL */
12469
12470#ifdef TERMSIZE_USE_CONIO
12471 {
12472 DWORD nhandle;
12473 HANDLE handle;
12474 CONSOLE_SCREEN_BUFFER_INFO csbi;
12475 switch (fd) {
12476 case 0: nhandle = STD_INPUT_HANDLE;
12477 break;
12478 case 1: nhandle = STD_OUTPUT_HANDLE;
12479 break;
12480 case 2: nhandle = STD_ERROR_HANDLE;
12481 break;
12482 default:
12483 return PyErr_Format(PyExc_ValueError, "bad file descriptor");
12484 }
12485 handle = GetStdHandle(nhandle);
12486 if (handle == NULL)
12487 return PyErr_Format(PyExc_OSError, "handle cannot be retrieved");
12488 if (handle == INVALID_HANDLE_VALUE)
12489 return PyErr_SetFromWindowsErr(0);
12490
12491 if (!GetConsoleScreenBufferInfo(handle, &csbi))
12492 return PyErr_SetFromWindowsErr(0);
12493
12494 columns = csbi.srWindow.Right - csbi.srWindow.Left + 1;
12495 lines = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
12496 }
12497#endif /* TERMSIZE_USE_CONIO */
12498
Serhiy Storchaka2b560312020-04-18 19:14:10 +030012499 PyObject *TerminalSizeType = get_posix_state(module)->TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080012500 termsize = PyStructSequence_New((PyTypeObject *)TerminalSizeType);
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012501 if (termsize == NULL)
12502 return NULL;
12503 PyStructSequence_SET_ITEM(termsize, 0, PyLong_FromLong(columns));
12504 PyStructSequence_SET_ITEM(termsize, 1, PyLong_FromLong(lines));
12505 if (PyErr_Occurred()) {
12506 Py_DECREF(termsize);
12507 return NULL;
12508 }
12509 return termsize;
12510}
12511#endif /* defined(TERMSIZE_USE_CONIO) || defined(TERMSIZE_USE_IOCTL) */
12512
Larry Hastings2f936352014-08-05 14:04:04 +100012513
12514/*[clinic input]
12515os.cpu_count
12516
Charles-François Natali80d62e62015-08-13 20:37:08 +010012517Return the number of CPUs in the system; return None if indeterminable.
12518
12519This number is not equivalent to the number of CPUs the current process can
12520use. The number of usable CPUs can be obtained with
12521``len(os.sched_getaffinity(0))``
Larry Hastings2f936352014-08-05 14:04:04 +100012522[clinic start generated code]*/
12523
Larry Hastings2f936352014-08-05 14:04:04 +100012524static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012525os_cpu_count_impl(PyObject *module)
Serhiy Storchaka2954f832016-07-07 18:20:03 +030012526/*[clinic end generated code: output=5fc29463c3936a9c input=e7c8f4ba6dbbadd3]*/
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012527{
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012528 int ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012529#ifdef MS_WINDOWS
Steve Doweraa929272019-09-11 16:15:39 +010012530 ncpu = GetActiveProcessorCount(ALL_PROCESSOR_GROUPS);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012531#elif defined(__hpux)
12532 ncpu = mpctl(MPC_GETNUMSPUS, NULL, NULL);
12533#elif defined(HAVE_SYSCONF) && defined(_SC_NPROCESSORS_ONLN)
12534 ncpu = sysconf(_SC_NPROCESSORS_ONLN);
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012535#elif defined(__DragonFly__) || \
12536 defined(__OpenBSD__) || \
12537 defined(__FreeBSD__) || \
Charles-Francois Natalid59087d2013-05-20 17:31:06 +020012538 defined(__NetBSD__) || \
12539 defined(__APPLE__)
Charles-Francois Natali7c4f8da2013-05-20 17:40:32 +020012540 int mib[2];
12541 size_t len = sizeof(ncpu);
12542 mib[0] = CTL_HW;
12543 mib[1] = HW_NCPU;
12544 if (sysctl(mib, 2, &ncpu, &len, NULL, 0) != 0)
12545 ncpu = 0;
Charles-Francois Natali44feda32013-05-20 14:40:46 +020012546#endif
12547 if (ncpu >= 1)
12548 return PyLong_FromLong(ncpu);
12549 else
12550 Py_RETURN_NONE;
12551}
12552
Victor Stinnerdaf45552013-08-28 00:53:59 +020012553
Larry Hastings2f936352014-08-05 14:04:04 +100012554/*[clinic input]
12555os.get_inheritable -> bool
12556
12557 fd: int
12558 /
12559
12560Get the close-on-exe flag of the specified file descriptor.
12561[clinic start generated code]*/
12562
Larry Hastings2f936352014-08-05 14:04:04 +100012563static int
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012564os_get_inheritable_impl(PyObject *module, int fd)
12565/*[clinic end generated code: output=0445e20e149aa5b8 input=89ac008dc9ab6b95]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012566{
Steve Dower8fc89802015-04-12 00:26:27 -040012567 int return_value;
Steve Dower8fc89802015-04-12 00:26:27 -040012568 _Py_BEGIN_SUPPRESS_IPH
12569 return_value = _Py_get_inheritable(fd);
12570 _Py_END_SUPPRESS_IPH
12571 return return_value;
Larry Hastings2f936352014-08-05 14:04:04 +100012572}
12573
12574
12575/*[clinic input]
12576os.set_inheritable
12577 fd: int
12578 inheritable: int
12579 /
12580
12581Set the inheritable flag of the specified file descriptor.
12582[clinic start generated code]*/
12583
Larry Hastings2f936352014-08-05 14:04:04 +100012584static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030012585os_set_inheritable_impl(PyObject *module, int fd, int inheritable)
12586/*[clinic end generated code: output=f1b1918a2f3c38c2 input=9ceaead87a1e2402]*/
Victor Stinnerdaf45552013-08-28 00:53:59 +020012587{
Steve Dower8fc89802015-04-12 00:26:27 -040012588 int result;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012589
Steve Dower8fc89802015-04-12 00:26:27 -040012590 _Py_BEGIN_SUPPRESS_IPH
12591 result = _Py_set_inheritable(fd, inheritable, NULL);
12592 _Py_END_SUPPRESS_IPH
12593 if (result < 0)
Victor Stinnerdaf45552013-08-28 00:53:59 +020012594 return NULL;
12595 Py_RETURN_NONE;
12596}
12597
12598
12599#ifdef MS_WINDOWS
Larry Hastings2f936352014-08-05 14:04:04 +100012600/*[clinic input]
12601os.get_handle_inheritable -> bool
Benjamin Petersonca470632016-09-06 13:47:26 -070012602 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012603 /
Victor Stinnerdaf45552013-08-28 00:53:59 +020012604
Larry Hastings2f936352014-08-05 14:04:04 +100012605Get the close-on-exe flag of the specified file descriptor.
12606[clinic start generated code]*/
12607
Larry Hastings2f936352014-08-05 14:04:04 +100012608static int
Benjamin Petersonca470632016-09-06 13:47:26 -070012609os_get_handle_inheritable_impl(PyObject *module, intptr_t handle)
Victor Stinner581139c2016-09-06 15:54:20 -070012610/*[clinic end generated code: output=36be5afca6ea84d8 input=cfe99f9c05c70ad1]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012611{
12612 DWORD flags;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012613
12614 if (!GetHandleInformation((HANDLE)handle, &flags)) {
12615 PyErr_SetFromWindowsErr(0);
Larry Hastings2f936352014-08-05 14:04:04 +100012616 return -1;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012617 }
12618
Larry Hastings2f936352014-08-05 14:04:04 +100012619 return flags & HANDLE_FLAG_INHERIT;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012620}
12621
Victor Stinnerdaf45552013-08-28 00:53:59 +020012622
Larry Hastings2f936352014-08-05 14:04:04 +100012623/*[clinic input]
12624os.set_handle_inheritable
Benjamin Petersonca470632016-09-06 13:47:26 -070012625 handle: intptr_t
Larry Hastings2f936352014-08-05 14:04:04 +100012626 inheritable: bool
12627 /
12628
12629Set the inheritable flag of the specified handle.
12630[clinic start generated code]*/
12631
Larry Hastings2f936352014-08-05 14:04:04 +100012632static PyObject *
Benjamin Petersonca470632016-09-06 13:47:26 -070012633os_set_handle_inheritable_impl(PyObject *module, intptr_t handle,
Larry Hastings89964c42015-04-14 18:07:59 -040012634 int inheritable)
Victor Stinner581139c2016-09-06 15:54:20 -070012635/*[clinic end generated code: output=021d74fe6c96baa3 input=7a7641390d8364fc]*/
Larry Hastings2f936352014-08-05 14:04:04 +100012636{
12637 DWORD flags = inheritable ? HANDLE_FLAG_INHERIT : 0;
Victor Stinnerdaf45552013-08-28 00:53:59 +020012638 if (!SetHandleInformation((HANDLE)handle, HANDLE_FLAG_INHERIT, flags)) {
12639 PyErr_SetFromWindowsErr(0);
12640 return NULL;
12641 }
12642 Py_RETURN_NONE;
12643}
Larry Hastings2f936352014-08-05 14:04:04 +100012644#endif /* MS_WINDOWS */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010012645
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012646#ifndef MS_WINDOWS
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012647/*[clinic input]
12648os.get_blocking -> bool
12649 fd: int
12650 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012651
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012652Get the blocking mode of the file descriptor.
12653
12654Return False if the O_NONBLOCK flag is set, True if the flag is cleared.
12655[clinic start generated code]*/
12656
12657static int
12658os_get_blocking_impl(PyObject *module, int fd)
12659/*[clinic end generated code: output=336a12ad76a61482 input=f4afb59d51560179]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012660{
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012661 int blocking;
12662
Steve Dower8fc89802015-04-12 00:26:27 -040012663 _Py_BEGIN_SUPPRESS_IPH
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012664 blocking = _Py_get_blocking(fd);
Steve Dower8fc89802015-04-12 00:26:27 -040012665 _Py_END_SUPPRESS_IPH
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012666 return blocking;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012667}
12668
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012669/*[clinic input]
12670os.set_blocking
12671 fd: int
12672 blocking: bool(accept={int})
12673 /
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012674
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012675Set the blocking mode of the specified file descriptor.
12676
12677Set the O_NONBLOCK flag if blocking is False,
12678clear the O_NONBLOCK flag otherwise.
12679[clinic start generated code]*/
12680
12681static PyObject *
12682os_set_blocking_impl(PyObject *module, int fd, int blocking)
12683/*[clinic end generated code: output=384eb43aa0762a9d input=bf5c8efdc5860ff3]*/
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012684{
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030012685 int result;
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012686
Steve Dower8fc89802015-04-12 00:26:27 -040012687 _Py_BEGIN_SUPPRESS_IPH
12688 result = _Py_set_blocking(fd, blocking);
12689 _Py_END_SUPPRESS_IPH
12690 if (result < 0)
Victor Stinner1db9e7b2014-07-29 22:32:47 +020012691 return NULL;
12692 Py_RETURN_NONE;
12693}
12694#endif /* !MS_WINDOWS */
12695
12696
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012697/*[clinic input]
Eddie Elizondob3966632019-11-05 07:16:14 -080012698class os.DirEntry "DirEntry *" "DirEntryType"
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012699[clinic start generated code]*/
Eddie Elizondob3966632019-11-05 07:16:14 -080012700/*[clinic end generated code: output=da39a3ee5e6b4b0d input=3c18c7a448247980]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012701
12702typedef struct {
12703 PyObject_HEAD
12704 PyObject *name;
12705 PyObject *path;
12706 PyObject *stat;
12707 PyObject *lstat;
12708#ifdef MS_WINDOWS
12709 struct _Py_stat_struct win32_lstat;
Victor Stinner0f6d7332017-03-09 17:34:28 +010012710 uint64_t win32_file_index;
Victor Stinner6036e442015-03-08 01:58:04 +010012711 int got_file_index;
12712#else /* POSIX */
Victor Stinner35a97c02015-03-08 02:59:09 +010012713#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010012714 unsigned char d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010012715#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012716 ino_t d_ino;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012717 int dir_fd;
Victor Stinner6036e442015-03-08 01:58:04 +010012718#endif
12719} DirEntry;
12720
Eddie Elizondob3966632019-11-05 07:16:14 -080012721static PyObject *
12722_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
12723{
12724 PyErr_Format(PyExc_TypeError,
12725 "cannot create '%.100s' instances", _PyType_Name(type));
12726 return NULL;
12727}
12728
Victor Stinner6036e442015-03-08 01:58:04 +010012729static void
12730DirEntry_dealloc(DirEntry *entry)
12731{
Eddie Elizondob3966632019-11-05 07:16:14 -080012732 PyTypeObject *tp = Py_TYPE(entry);
Victor Stinner6036e442015-03-08 01:58:04 +010012733 Py_XDECREF(entry->name);
12734 Py_XDECREF(entry->path);
12735 Py_XDECREF(entry->stat);
12736 Py_XDECREF(entry->lstat);
Eddie Elizondob3966632019-11-05 07:16:14 -080012737 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
12738 free_func(entry);
12739 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010012740}
12741
12742/* Forward reference */
12743static int
12744DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits);
12745
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012746/*[clinic input]
12747os.DirEntry.is_symlink -> bool
12748
12749Return True if the entry is a symbolic link; cached per entry.
12750[clinic start generated code]*/
12751
Victor Stinner6036e442015-03-08 01:58:04 +010012752static int
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012753os_DirEntry_is_symlink_impl(DirEntry *self)
12754/*[clinic end generated code: output=42244667d7bcfc25 input=1605a1b4b96976c3]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012755{
12756#ifdef MS_WINDOWS
12757 return (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
Victor Stinner35a97c02015-03-08 02:59:09 +010012758#elif defined(HAVE_DIRENT_D_TYPE)
12759 /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010012760 if (self->d_type != DT_UNKNOWN)
12761 return self->d_type == DT_LNK;
12762 else
12763 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner35a97c02015-03-08 02:59:09 +010012764#else
12765 /* POSIX without d_type */
12766 return DirEntry_test_mode(self, 0, S_IFLNK);
Victor Stinner6036e442015-03-08 01:58:04 +010012767#endif
12768}
12769
12770static PyObject *
Victor Stinner6036e442015-03-08 01:58:04 +010012771DirEntry_fetch_stat(DirEntry *self, int follow_symlinks)
12772{
12773 int result;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012774 STRUCT_STAT st;
12775 PyObject *ub;
Victor Stinner6036e442015-03-08 01:58:04 +010012776
12777#ifdef MS_WINDOWS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012778 if (!PyUnicode_FSDecoder(self->path, &ub))
12779 return NULL;
12780 const wchar_t *path = PyUnicode_AsUnicode(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012781#else /* POSIX */
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012782 if (!PyUnicode_FSConverter(self->path, &ub))
12783 return NULL;
12784 const char *path = PyBytes_AS_STRING(ub);
12785 if (self->dir_fd != DEFAULT_DIR_FD) {
12786#ifdef HAVE_FSTATAT
12787 result = fstatat(self->dir_fd, path, &st,
12788 follow_symlinks ? 0 : AT_SYMLINK_NOFOLLOW);
12789#else
12790 PyErr_SetString(PyExc_NotImplementedError, "can't fetch stat");
12791 return NULL;
12792#endif /* HAVE_FSTATAT */
12793 }
12794 else
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012795#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012796 {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012797 if (follow_symlinks)
12798 result = STAT(path, &st);
12799 else
12800 result = LSTAT(path, &st);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030012801 }
12802 Py_DECREF(ub);
Victor Stinner6036e442015-03-08 01:58:04 +010012803
12804 if (result != 0)
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012805 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012806
12807 return _pystat_fromstructstat(&st);
12808}
12809
12810static PyObject *
12811DirEntry_get_lstat(DirEntry *self)
12812{
12813 if (!self->lstat) {
12814#ifdef MS_WINDOWS
12815 self->lstat = _pystat_fromstructstat(&self->win32_lstat);
12816#else /* POSIX */
12817 self->lstat = DirEntry_fetch_stat(self, 0);
12818#endif
12819 }
12820 Py_XINCREF(self->lstat);
12821 return self->lstat;
12822}
12823
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012824/*[clinic input]
12825os.DirEntry.stat
12826 *
12827 follow_symlinks: bool = True
12828
12829Return stat_result object for the entry; cached per entry.
12830[clinic start generated code]*/
12831
Victor Stinner6036e442015-03-08 01:58:04 +010012832static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012833os_DirEntry_stat_impl(DirEntry *self, int follow_symlinks)
12834/*[clinic end generated code: output=008593b3a6d01305 input=280d14c1d6f1d00d]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012835{
12836 if (!follow_symlinks)
12837 return DirEntry_get_lstat(self);
12838
12839 if (!self->stat) {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012840 int result = os_DirEntry_is_symlink_impl(self);
Victor Stinner6036e442015-03-08 01:58:04 +010012841 if (result == -1)
12842 return NULL;
12843 else if (result)
12844 self->stat = DirEntry_fetch_stat(self, 1);
12845 else
12846 self->stat = DirEntry_get_lstat(self);
12847 }
12848
12849 Py_XINCREF(self->stat);
12850 return self->stat;
12851}
12852
Victor Stinner6036e442015-03-08 01:58:04 +010012853/* Set exception and return -1 on error, 0 for False, 1 for True */
12854static int
12855DirEntry_test_mode(DirEntry *self, int follow_symlinks, unsigned short mode_bits)
12856{
12857 PyObject *stat = NULL;
12858 PyObject *st_mode = NULL;
12859 long mode;
12860 int result;
Victor Stinner35a97c02015-03-08 02:59:09 +010012861#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012862 int is_symlink;
12863 int need_stat;
Victor Stinner35a97c02015-03-08 02:59:09 +010012864#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012865#ifdef MS_WINDOWS
12866 unsigned long dir_bits;
12867#endif
12868
12869#ifdef MS_WINDOWS
12870 is_symlink = (self->win32_lstat.st_mode & S_IFMT) == S_IFLNK;
12871 need_stat = follow_symlinks && is_symlink;
Victor Stinner35a97c02015-03-08 02:59:09 +010012872#elif defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012873 is_symlink = self->d_type == DT_LNK;
12874 need_stat = self->d_type == DT_UNKNOWN || (follow_symlinks && is_symlink);
12875#endif
12876
Victor Stinner35a97c02015-03-08 02:59:09 +010012877#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012878 if (need_stat) {
Victor Stinner35a97c02015-03-08 02:59:09 +010012879#endif
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012880 stat = os_DirEntry_stat_impl(self, follow_symlinks);
Victor Stinner6036e442015-03-08 01:58:04 +010012881 if (!stat) {
12882 if (PyErr_ExceptionMatches(PyExc_FileNotFoundError)) {
12883 /* If file doesn't exist (anymore), then return False
12884 (i.e., say it's not a file/directory) */
12885 PyErr_Clear();
12886 return 0;
12887 }
12888 goto error;
12889 }
Eddie Elizondob3966632019-11-05 07:16:14 -080012890 st_mode = PyObject_GetAttr(stat, _posixstate_global->st_mode);
Victor Stinner6036e442015-03-08 01:58:04 +010012891 if (!st_mode)
12892 goto error;
12893
12894 mode = PyLong_AsLong(st_mode);
12895 if (mode == -1 && PyErr_Occurred())
12896 goto error;
12897 Py_CLEAR(st_mode);
12898 Py_CLEAR(stat);
12899 result = (mode & S_IFMT) == mode_bits;
Victor Stinner35a97c02015-03-08 02:59:09 +010012900#if defined(MS_WINDOWS) || defined(HAVE_DIRENT_D_TYPE)
Victor Stinner6036e442015-03-08 01:58:04 +010012901 }
12902 else if (is_symlink) {
12903 assert(mode_bits != S_IFLNK);
12904 result = 0;
12905 }
12906 else {
12907 assert(mode_bits == S_IFDIR || mode_bits == S_IFREG);
12908#ifdef MS_WINDOWS
12909 dir_bits = self->win32_lstat.st_file_attributes & FILE_ATTRIBUTE_DIRECTORY;
12910 if (mode_bits == S_IFDIR)
12911 result = dir_bits != 0;
12912 else
12913 result = dir_bits == 0;
12914#else /* POSIX */
12915 if (mode_bits == S_IFDIR)
12916 result = self->d_type == DT_DIR;
12917 else
12918 result = self->d_type == DT_REG;
12919#endif
12920 }
Victor Stinner35a97c02015-03-08 02:59:09 +010012921#endif
Victor Stinner6036e442015-03-08 01:58:04 +010012922
12923 return result;
12924
12925error:
12926 Py_XDECREF(st_mode);
12927 Py_XDECREF(stat);
12928 return -1;
12929}
12930
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012931/*[clinic input]
12932os.DirEntry.is_dir -> bool
12933 *
12934 follow_symlinks: bool = True
Victor Stinner6036e442015-03-08 01:58:04 +010012935
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012936Return True if the entry is a directory; cached per entry.
12937[clinic start generated code]*/
12938
12939static int
12940os_DirEntry_is_dir_impl(DirEntry *self, int follow_symlinks)
12941/*[clinic end generated code: output=ad2e8d54365da287 input=0135232766f53f58]*/
12942{
12943 return DirEntry_test_mode(self, follow_symlinks, S_IFDIR);
Victor Stinner6036e442015-03-08 01:58:04 +010012944}
12945
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012946/*[clinic input]
12947os.DirEntry.is_file -> bool
12948 *
12949 follow_symlinks: bool = True
12950
12951Return True if the entry is a file; cached per entry.
12952[clinic start generated code]*/
12953
12954static int
12955os_DirEntry_is_file_impl(DirEntry *self, int follow_symlinks)
12956/*[clinic end generated code: output=8462ade481d8a476 input=0dc90be168b041ee]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012957{
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012958 return DirEntry_test_mode(self, follow_symlinks, S_IFREG);
Victor Stinner6036e442015-03-08 01:58:04 +010012959}
12960
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012961/*[clinic input]
12962os.DirEntry.inode
Victor Stinner6036e442015-03-08 01:58:04 +010012963
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012964Return inode of the entry; cached per entry.
12965[clinic start generated code]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012966
12967static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020012968os_DirEntry_inode_impl(DirEntry *self)
12969/*[clinic end generated code: output=156bb3a72162440e input=3ee7b872ae8649f0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010012970{
12971#ifdef MS_WINDOWS
12972 if (!self->got_file_index) {
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012973 PyObject *unicode;
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030012974 const wchar_t *path;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012975 STRUCT_STAT stat;
12976 int result;
Victor Stinner6036e442015-03-08 01:58:04 +010012977
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012978 if (!PyUnicode_FSDecoder(self->path, &unicode))
Victor Stinner6036e442015-03-08 01:58:04 +010012979 return NULL;
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012980 path = PyUnicode_AsUnicode(unicode);
12981 result = LSTAT(path, &stat);
12982 Py_DECREF(unicode);
Victor Stinner6036e442015-03-08 01:58:04 +010012983
Serhiy Storchaka2674bc72016-10-08 20:16:57 +030012984 if (result != 0)
12985 return path_object_error(self->path);
Victor Stinner6036e442015-03-08 01:58:04 +010012986
12987 self->win32_file_index = stat.st_ino;
12988 self->got_file_index = 1;
12989 }
Victor Stinner0f6d7332017-03-09 17:34:28 +010012990 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->win32_file_index));
12991 return PyLong_FromUnsignedLongLong(self->win32_file_index);
Victor Stinner6036e442015-03-08 01:58:04 +010012992#else /* POSIX */
xdegaye50e86032017-05-22 11:15:08 +020012993 Py_BUILD_ASSERT(sizeof(unsigned long long) >= sizeof(self->d_ino));
12994 return PyLong_FromUnsignedLongLong(self->d_ino);
Victor Stinner6036e442015-03-08 01:58:04 +010012995#endif
12996}
12997
12998static PyObject *
12999DirEntry_repr(DirEntry *self)
13000{
13001 return PyUnicode_FromFormat("<DirEntry %R>", self->name);
13002}
13003
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013004/*[clinic input]
13005os.DirEntry.__fspath__
13006
13007Returns the path for the entry.
13008[clinic start generated code]*/
13009
Brett Cannon96881cd2016-06-10 14:37:21 -070013010static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013011os_DirEntry___fspath___impl(DirEntry *self)
13012/*[clinic end generated code: output=6dd7f7ef752e6f4f input=3c49d0cf38df4fac]*/
Brett Cannon96881cd2016-06-10 14:37:21 -070013013{
13014 Py_INCREF(self->path);
13015 return self->path;
13016}
13017
Victor Stinner6036e442015-03-08 01:58:04 +010013018static PyMemberDef DirEntry_members[] = {
13019 {"name", T_OBJECT_EX, offsetof(DirEntry, name), READONLY,
13020 "the entry's base filename, relative to scandir() \"path\" argument"},
13021 {"path", T_OBJECT_EX, offsetof(DirEntry, path), READONLY,
13022 "the entry's full path name; equivalent to os.path.join(scandir_path, entry.name)"},
13023 {NULL}
13024};
13025
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013026#include "clinic/posixmodule.c.h"
13027
Victor Stinner6036e442015-03-08 01:58:04 +010013028static PyMethodDef DirEntry_methods[] = {
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013029 OS_DIRENTRY_IS_DIR_METHODDEF
13030 OS_DIRENTRY_IS_FILE_METHODDEF
13031 OS_DIRENTRY_IS_SYMLINK_METHODDEF
13032 OS_DIRENTRY_STAT_METHODDEF
13033 OS_DIRENTRY_INODE_METHODDEF
13034 OS_DIRENTRY___FSPATH___METHODDEF
Batuhan Taşkayaf9dd51e2020-04-08 00:37:19 +030013035 {"__class_getitem__", (PyCFunction)Py_GenericAlias,
13036 METH_O|METH_CLASS, PyDoc_STR("See PEP 585")},
Victor Stinner6036e442015-03-08 01:58:04 +010013037 {NULL}
13038};
13039
Eddie Elizondob3966632019-11-05 07:16:14 -080013040static PyType_Slot DirEntryType_slots[] = {
13041 {Py_tp_new, _disabled_new},
13042 {Py_tp_dealloc, DirEntry_dealloc},
13043 {Py_tp_repr, DirEntry_repr},
13044 {Py_tp_methods, DirEntry_methods},
13045 {Py_tp_members, DirEntry_members},
13046 {0, 0},
Victor Stinner6036e442015-03-08 01:58:04 +010013047};
13048
Eddie Elizondob3966632019-11-05 07:16:14 -080013049static PyType_Spec DirEntryType_spec = {
13050 MODNAME ".DirEntry",
13051 sizeof(DirEntry),
13052 0,
13053 Py_TPFLAGS_DEFAULT,
13054 DirEntryType_slots
13055};
13056
13057
Victor Stinner6036e442015-03-08 01:58:04 +010013058#ifdef MS_WINDOWS
13059
13060static wchar_t *
Serhiy Storchakadeab18d2016-05-07 16:45:18 +030013061join_path_filenameW(const wchar_t *path_wide, const wchar_t *filename)
Victor Stinner6036e442015-03-08 01:58:04 +010013062{
13063 Py_ssize_t path_len;
13064 Py_ssize_t size;
13065 wchar_t *result;
13066 wchar_t ch;
13067
13068 if (!path_wide) { /* Default arg: "." */
13069 path_wide = L".";
13070 path_len = 1;
13071 }
13072 else {
13073 path_len = wcslen(path_wide);
13074 }
13075
13076 /* The +1's are for the path separator and the NUL */
13077 size = path_len + 1 + wcslen(filename) + 1;
13078 result = PyMem_New(wchar_t, size);
13079 if (!result) {
13080 PyErr_NoMemory();
13081 return NULL;
13082 }
13083 wcscpy(result, path_wide);
13084 if (path_len > 0) {
13085 ch = result[path_len - 1];
13086 if (ch != SEP && ch != ALTSEP && ch != L':')
13087 result[path_len++] = SEP;
13088 wcscpy(result + path_len, filename);
13089 }
13090 return result;
13091}
13092
13093static PyObject *
13094DirEntry_from_find_data(path_t *path, WIN32_FIND_DATAW *dataW)
13095{
13096 DirEntry *entry;
13097 BY_HANDLE_FILE_INFORMATION file_info;
13098 ULONG reparse_tag;
13099 wchar_t *joined_path;
13100
Eddie Elizondob3966632019-11-05 07:16:14 -080013101 PyObject *DirEntryType = _posixstate_global->DirEntryType;
13102 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013103 if (!entry)
13104 return NULL;
13105 entry->name = NULL;
13106 entry->path = NULL;
13107 entry->stat = NULL;
13108 entry->lstat = NULL;
13109 entry->got_file_index = 0;
13110
13111 entry->name = PyUnicode_FromWideChar(dataW->cFileName, -1);
13112 if (!entry->name)
13113 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013114 if (path->narrow) {
13115 Py_SETREF(entry->name, PyUnicode_EncodeFSDefault(entry->name));
13116 if (!entry->name)
13117 goto error;
13118 }
Victor Stinner6036e442015-03-08 01:58:04 +010013119
13120 joined_path = join_path_filenameW(path->wide, dataW->cFileName);
13121 if (!joined_path)
13122 goto error;
13123
13124 entry->path = PyUnicode_FromWideChar(joined_path, -1);
13125 PyMem_Free(joined_path);
13126 if (!entry->path)
13127 goto error;
Steve Dowercc16be82016-09-08 10:35:16 -070013128 if (path->narrow) {
13129 Py_SETREF(entry->path, PyUnicode_EncodeFSDefault(entry->path));
13130 if (!entry->path)
13131 goto error;
13132 }
Victor Stinner6036e442015-03-08 01:58:04 +010013133
Steve Dowercc16be82016-09-08 10:35:16 -070013134 find_data_to_file_info(dataW, &file_info, &reparse_tag);
Victor Stinner6036e442015-03-08 01:58:04 +010013135 _Py_attribute_data_to_stat(&file_info, reparse_tag, &entry->win32_lstat);
13136
13137 return (PyObject *)entry;
13138
13139error:
13140 Py_DECREF(entry);
13141 return NULL;
13142}
13143
13144#else /* POSIX */
13145
13146static char *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013147join_path_filename(const char *path_narrow, const char* filename, Py_ssize_t filename_len)
Victor Stinner6036e442015-03-08 01:58:04 +010013148{
13149 Py_ssize_t path_len;
13150 Py_ssize_t size;
13151 char *result;
13152
13153 if (!path_narrow) { /* Default arg: "." */
13154 path_narrow = ".";
13155 path_len = 1;
13156 }
13157 else {
13158 path_len = strlen(path_narrow);
13159 }
13160
13161 if (filename_len == -1)
13162 filename_len = strlen(filename);
13163
13164 /* The +1's are for the path separator and the NUL */
13165 size = path_len + 1 + filename_len + 1;
13166 result = PyMem_New(char, size);
13167 if (!result) {
13168 PyErr_NoMemory();
13169 return NULL;
13170 }
13171 strcpy(result, path_narrow);
13172 if (path_len > 0 && result[path_len - 1] != '/')
13173 result[path_len++] = '/';
13174 strcpy(result + path_len, filename);
13175 return result;
13176}
13177
13178static PyObject *
Serhiy Storchakaef1585e2015-12-25 20:01:53 +020013179DirEntry_from_posix_info(path_t *path, const char *name, Py_ssize_t name_len,
Victor Stinner35a97c02015-03-08 02:59:09 +010013180 ino_t d_ino
13181#ifdef HAVE_DIRENT_D_TYPE
13182 , unsigned char d_type
13183#endif
13184 )
Victor Stinner6036e442015-03-08 01:58:04 +010013185{
13186 DirEntry *entry;
13187 char *joined_path;
13188
Eddie Elizondob3966632019-11-05 07:16:14 -080013189 PyObject *DirEntryType = _posixstate_global->DirEntryType;
13190 entry = PyObject_New(DirEntry, (PyTypeObject *)DirEntryType);
Victor Stinner6036e442015-03-08 01:58:04 +010013191 if (!entry)
13192 return NULL;
13193 entry->name = NULL;
13194 entry->path = NULL;
13195 entry->stat = NULL;
13196 entry->lstat = NULL;
13197
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013198 if (path->fd != -1) {
13199 entry->dir_fd = path->fd;
13200 joined_path = NULL;
13201 }
13202 else {
13203 entry->dir_fd = DEFAULT_DIR_FD;
13204 joined_path = join_path_filename(path->narrow, name, name_len);
13205 if (!joined_path)
13206 goto error;
13207 }
Victor Stinner6036e442015-03-08 01:58:04 +010013208
Serhiy Storchaka1180e5a2017-07-11 06:36:46 +030013209 if (!path->narrow || !PyObject_CheckBuffer(path->object)) {
Victor Stinner6036e442015-03-08 01:58:04 +010013210 entry->name = PyUnicode_DecodeFSDefaultAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013211 if (joined_path)
13212 entry->path = PyUnicode_DecodeFSDefault(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013213 }
13214 else {
13215 entry->name = PyBytes_FromStringAndSize(name, name_len);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013216 if (joined_path)
13217 entry->path = PyBytes_FromString(joined_path);
Victor Stinner6036e442015-03-08 01:58:04 +010013218 }
13219 PyMem_Free(joined_path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013220 if (!entry->name)
13221 goto error;
13222
13223 if (path->fd != -1) {
13224 entry->path = entry->name;
13225 Py_INCREF(entry->path);
13226 }
13227 else if (!entry->path)
Victor Stinner6036e442015-03-08 01:58:04 +010013228 goto error;
13229
Victor Stinner35a97c02015-03-08 02:59:09 +010013230#ifdef HAVE_DIRENT_D_TYPE
Victor Stinner6036e442015-03-08 01:58:04 +010013231 entry->d_type = d_type;
Victor Stinner35a97c02015-03-08 02:59:09 +010013232#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013233 entry->d_ino = d_ino;
13234
13235 return (PyObject *)entry;
13236
13237error:
13238 Py_XDECREF(entry);
13239 return NULL;
13240}
13241
13242#endif
13243
13244
13245typedef struct {
13246 PyObject_HEAD
13247 path_t path;
13248#ifdef MS_WINDOWS
13249 HANDLE handle;
13250 WIN32_FIND_DATAW file_data;
13251 int first_time;
13252#else /* POSIX */
13253 DIR *dirp;
13254#endif
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013255#ifdef HAVE_FDOPENDIR
13256 int fd;
13257#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013258} ScandirIterator;
13259
13260#ifdef MS_WINDOWS
13261
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013262static int
13263ScandirIterator_is_closed(ScandirIterator *iterator)
13264{
13265 return iterator->handle == INVALID_HANDLE_VALUE;
13266}
13267
Victor Stinner6036e442015-03-08 01:58:04 +010013268static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013269ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013270{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013271 HANDLE handle = iterator->handle;
13272
13273 if (handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013274 return;
13275
Victor Stinner6036e442015-03-08 01:58:04 +010013276 iterator->handle = INVALID_HANDLE_VALUE;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013277 Py_BEGIN_ALLOW_THREADS
13278 FindClose(handle);
13279 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013280}
13281
13282static PyObject *
13283ScandirIterator_iternext(ScandirIterator *iterator)
13284{
13285 WIN32_FIND_DATAW *file_data = &iterator->file_data;
13286 BOOL success;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013287 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013288
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013289 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013290 if (iterator->handle == INVALID_HANDLE_VALUE)
Victor Stinner6036e442015-03-08 01:58:04 +010013291 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013292
13293 while (1) {
13294 if (!iterator->first_time) {
13295 Py_BEGIN_ALLOW_THREADS
13296 success = FindNextFileW(iterator->handle, file_data);
13297 Py_END_ALLOW_THREADS
13298 if (!success) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013299 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013300 if (GetLastError() != ERROR_NO_MORE_FILES)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013301 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013302 break;
13303 }
13304 }
13305 iterator->first_time = 0;
13306
13307 /* Skip over . and .. */
13308 if (wcscmp(file_data->cFileName, L".") != 0 &&
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013309 wcscmp(file_data->cFileName, L"..") != 0) {
13310 entry = DirEntry_from_find_data(&iterator->path, file_data);
13311 if (!entry)
13312 break;
13313 return entry;
13314 }
Victor Stinner6036e442015-03-08 01:58:04 +010013315
13316 /* Loop till we get a non-dot directory or finish iterating */
13317 }
13318
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013319 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013320 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013321 return NULL;
13322}
13323
13324#else /* POSIX */
13325
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013326static int
13327ScandirIterator_is_closed(ScandirIterator *iterator)
13328{
13329 return !iterator->dirp;
13330}
13331
Victor Stinner6036e442015-03-08 01:58:04 +010013332static void
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013333ScandirIterator_closedir(ScandirIterator *iterator)
Victor Stinner6036e442015-03-08 01:58:04 +010013334{
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013335 DIR *dirp = iterator->dirp;
13336
13337 if (!dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013338 return;
13339
Victor Stinner6036e442015-03-08 01:58:04 +010013340 iterator->dirp = NULL;
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013341 Py_BEGIN_ALLOW_THREADS
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013342#ifdef HAVE_FDOPENDIR
13343 if (iterator->path.fd != -1)
13344 rewinddir(dirp);
13345#endif
Serhiy Storchakafbb1c5e2016-03-30 20:40:02 +030013346 closedir(dirp);
13347 Py_END_ALLOW_THREADS
Victor Stinner6036e442015-03-08 01:58:04 +010013348 return;
13349}
13350
13351static PyObject *
13352ScandirIterator_iternext(ScandirIterator *iterator)
13353{
13354 struct dirent *direntp;
13355 Py_ssize_t name_len;
13356 int is_dot;
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013357 PyObject *entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013358
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013359 /* Happens if the iterator is iterated twice, or closed explicitly */
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013360 if (!iterator->dirp)
Victor Stinner6036e442015-03-08 01:58:04 +010013361 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013362
13363 while (1) {
13364 errno = 0;
13365 Py_BEGIN_ALLOW_THREADS
13366 direntp = readdir(iterator->dirp);
13367 Py_END_ALLOW_THREADS
13368
13369 if (!direntp) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013370 /* Error or no more files */
Victor Stinner6036e442015-03-08 01:58:04 +010013371 if (errno != 0)
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013372 path_error(&iterator->path);
Victor Stinner6036e442015-03-08 01:58:04 +010013373 break;
13374 }
13375
13376 /* Skip over . and .. */
13377 name_len = NAMLEN(direntp);
13378 is_dot = direntp->d_name[0] == '.' &&
13379 (name_len == 1 || (direntp->d_name[1] == '.' && name_len == 2));
13380 if (!is_dot) {
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013381 entry = DirEntry_from_posix_info(&iterator->path, direntp->d_name,
Victor Stinner35a97c02015-03-08 02:59:09 +010013382 name_len, direntp->d_ino
13383#ifdef HAVE_DIRENT_D_TYPE
13384 , direntp->d_type
13385#endif
13386 );
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013387 if (!entry)
13388 break;
13389 return entry;
Victor Stinner6036e442015-03-08 01:58:04 +010013390 }
13391
13392 /* Loop till we get a non-dot directory or finish iterating */
13393 }
13394
Serhiy Storchaka988b9bc2016-02-08 17:56:36 +020013395 /* Error or no more files */
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013396 ScandirIterator_closedir(iterator);
Victor Stinner6036e442015-03-08 01:58:04 +010013397 return NULL;
13398}
13399
13400#endif
13401
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013402static PyObject *
13403ScandirIterator_close(ScandirIterator *self, PyObject *args)
13404{
13405 ScandirIterator_closedir(self);
13406 Py_RETURN_NONE;
13407}
13408
13409static PyObject *
13410ScandirIterator_enter(PyObject *self, PyObject *args)
13411{
13412 Py_INCREF(self);
13413 return self;
13414}
13415
13416static PyObject *
13417ScandirIterator_exit(ScandirIterator *self, PyObject *args)
13418{
13419 ScandirIterator_closedir(self);
13420 Py_RETURN_NONE;
13421}
13422
Victor Stinner6036e442015-03-08 01:58:04 +010013423static void
Victor Stinner7bfa4092016-03-23 00:43:54 +010013424ScandirIterator_finalize(ScandirIterator *iterator)
13425{
13426 PyObject *error_type, *error_value, *error_traceback;
13427
13428 /* Save the current exception, if any. */
13429 PyErr_Fetch(&error_type, &error_value, &error_traceback);
13430
13431 if (!ScandirIterator_is_closed(iterator)) {
13432 ScandirIterator_closedir(iterator);
13433
13434 if (PyErr_ResourceWarning((PyObject *)iterator, 1,
13435 "unclosed scandir iterator %R", iterator)) {
13436 /* Spurious errors can appear at shutdown */
13437 if (PyErr_ExceptionMatches(PyExc_Warning)) {
13438 PyErr_WriteUnraisable((PyObject *) iterator);
13439 }
13440 }
13441 }
13442
Victor Stinner7bfa4092016-03-23 00:43:54 +010013443 path_cleanup(&iterator->path);
13444
13445 /* Restore the saved exception. */
13446 PyErr_Restore(error_type, error_value, error_traceback);
13447}
13448
13449static void
Victor Stinner6036e442015-03-08 01:58:04 +010013450ScandirIterator_dealloc(ScandirIterator *iterator)
13451{
Eddie Elizondob3966632019-11-05 07:16:14 -080013452 PyTypeObject *tp = Py_TYPE(iterator);
Victor Stinner7bfa4092016-03-23 00:43:54 +010013453 if (PyObject_CallFinalizerFromDealloc((PyObject *)iterator) < 0)
13454 return;
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013455
Eddie Elizondob3966632019-11-05 07:16:14 -080013456 freefunc free_func = PyType_GetSlot(tp, Py_tp_free);
13457 free_func(iterator);
13458 Py_DECREF(tp);
Victor Stinner6036e442015-03-08 01:58:04 +010013459}
13460
Serhiy Storchakaffe96ae2016-02-11 13:21:30 +020013461static PyMethodDef ScandirIterator_methods[] = {
13462 {"__enter__", (PyCFunction)ScandirIterator_enter, METH_NOARGS},
13463 {"__exit__", (PyCFunction)ScandirIterator_exit, METH_VARARGS},
13464 {"close", (PyCFunction)ScandirIterator_close, METH_NOARGS},
13465 {NULL}
13466};
13467
Eddie Elizondob3966632019-11-05 07:16:14 -080013468static PyType_Slot ScandirIteratorType_slots[] = {
13469 {Py_tp_new, _disabled_new},
13470 {Py_tp_dealloc, ScandirIterator_dealloc},
13471 {Py_tp_finalize, ScandirIterator_finalize},
13472 {Py_tp_iter, PyObject_SelfIter},
13473 {Py_tp_iternext, ScandirIterator_iternext},
13474 {Py_tp_methods, ScandirIterator_methods},
13475 {0, 0},
13476};
13477
13478static PyType_Spec ScandirIteratorType_spec = {
13479 MODNAME ".ScandirIterator",
13480 sizeof(ScandirIterator),
13481 0,
13482 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_FINALIZE,
13483 ScandirIteratorType_slots
Victor Stinner6036e442015-03-08 01:58:04 +010013484};
13485
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013486/*[clinic input]
13487os.scandir
13488
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013489 path : path_t(nullable=True, allow_fd='PATH_HAVE_FDOPENDIR') = None
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013490
13491Return an iterator of DirEntry objects for given path.
13492
BNMetricsb9427072018-11-02 15:20:19 +000013493path can be specified as either str, bytes, or a path-like object. If path
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013494is bytes, the names of yielded DirEntry objects will also be bytes; in
13495all other circumstances they will be str.
13496
13497If path is None, uses the path='.'.
13498[clinic start generated code]*/
13499
Victor Stinner6036e442015-03-08 01:58:04 +010013500static PyObject *
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013501os_scandir_impl(PyObject *module, path_t *path)
BNMetricsb9427072018-11-02 15:20:19 +000013502/*[clinic end generated code: output=6eb2668b675ca89e input=6bdd312708fc3bb0]*/
Victor Stinner6036e442015-03-08 01:58:04 +010013503{
13504 ScandirIterator *iterator;
Victor Stinner6036e442015-03-08 01:58:04 +010013505#ifdef MS_WINDOWS
13506 wchar_t *path_strW;
13507#else
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013508 const char *path_str;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013509#ifdef HAVE_FDOPENDIR
13510 int fd = -1;
13511#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013512#endif
13513
Steve Dower60419a72019-06-24 08:42:54 -070013514 if (PySys_Audit("os.scandir", "O",
13515 path->object ? path->object : Py_None) < 0) {
13516 return NULL;
13517 }
13518
Hai Shif707d942020-03-16 21:15:01 +080013519 PyObject *ScandirIteratorType = get_posix_state(module)->ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080013520 iterator = PyObject_New(ScandirIterator, (PyTypeObject *)ScandirIteratorType);
Victor Stinner6036e442015-03-08 01:58:04 +010013521 if (!iterator)
13522 return NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013523
13524#ifdef MS_WINDOWS
13525 iterator->handle = INVALID_HANDLE_VALUE;
13526#else
13527 iterator->dirp = NULL;
13528#endif
13529
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020013530 memcpy(&iterator->path, path, sizeof(path_t));
Serhiy Storchaka095ef732017-02-09 20:05:51 +020013531 /* Move the ownership to iterator->path */
13532 path->object = NULL;
13533 path->cleanup = NULL;
Victor Stinner6036e442015-03-08 01:58:04 +010013534
13535#ifdef MS_WINDOWS
Victor Stinner6036e442015-03-08 01:58:04 +010013536 iterator->first_time = 1;
13537
13538 path_strW = join_path_filenameW(iterator->path.wide, L"*.*");
13539 if (!path_strW)
13540 goto error;
13541
13542 Py_BEGIN_ALLOW_THREADS
13543 iterator->handle = FindFirstFileW(path_strW, &iterator->file_data);
13544 Py_END_ALLOW_THREADS
13545
13546 PyMem_Free(path_strW);
13547
13548 if (iterator->handle == INVALID_HANDLE_VALUE) {
13549 path_error(&iterator->path);
13550 goto error;
13551 }
13552#else /* POSIX */
Victor Stinner6036e442015-03-08 01:58:04 +010013553 errno = 0;
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013554#ifdef HAVE_FDOPENDIR
13555 if (path->fd != -1) {
13556 /* closedir() closes the FD, so we duplicate it */
13557 fd = _Py_dup(path->fd);
13558 if (fd == -1)
13559 goto error;
13560
13561 Py_BEGIN_ALLOW_THREADS
13562 iterator->dirp = fdopendir(fd);
13563 Py_END_ALLOW_THREADS
13564 }
13565 else
13566#endif
13567 {
13568 if (iterator->path.narrow)
13569 path_str = iterator->path.narrow;
13570 else
13571 path_str = ".";
13572
13573 Py_BEGIN_ALLOW_THREADS
13574 iterator->dirp = opendir(path_str);
13575 Py_END_ALLOW_THREADS
13576 }
Victor Stinner6036e442015-03-08 01:58:04 +010013577
13578 if (!iterator->dirp) {
13579 path_error(&iterator->path);
Serhiy Storchakaea720fe2017-03-30 09:12:31 +030013580#ifdef HAVE_FDOPENDIR
13581 if (fd != -1) {
13582 Py_BEGIN_ALLOW_THREADS
13583 close(fd);
13584 Py_END_ALLOW_THREADS
13585 }
13586#endif
Victor Stinner6036e442015-03-08 01:58:04 +010013587 goto error;
13588 }
13589#endif
13590
13591 return (PyObject *)iterator;
13592
13593error:
13594 Py_DECREF(iterator);
13595 return NULL;
13596}
13597
Ethan Furman410ef8e2016-06-04 12:06:26 -070013598/*
13599 Return the file system path representation of the object.
13600
13601 If the object is str or bytes, then allow it to pass through with
13602 an incremented refcount. If the object defines __fspath__(), then
13603 return the result of that method. All other types raise a TypeError.
13604*/
13605PyObject *
13606PyOS_FSPath(PyObject *path)
13607{
Brett Cannon3f9183b2016-08-26 14:44:48 -070013608 /* For error message reasons, this function is manually inlined in
13609 path_converter(). */
Ethan Furman410ef8e2016-06-04 12:06:26 -070013610 PyObject *func = NULL;
13611 PyObject *path_repr = NULL;
13612
13613 if (PyUnicode_Check(path) || PyBytes_Check(path)) {
13614 Py_INCREF(path);
13615 return path;
13616 }
13617
13618 func = _PyObject_LookupSpecial(path, &PyId___fspath__);
13619 if (NULL == func) {
13620 return PyErr_Format(PyExc_TypeError,
13621 "expected str, bytes or os.PathLike object, "
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013622 "not %.200s",
Eddie Elizondob3966632019-11-05 07:16:14 -080013623 _PyType_Name(Py_TYPE(path)));
Ethan Furman410ef8e2016-06-04 12:06:26 -070013624 }
13625
Victor Stinnerf17c3de2016-12-06 18:46:19 +010013626 path_repr = _PyObject_CallNoArg(func);
Ethan Furman410ef8e2016-06-04 12:06:26 -070013627 Py_DECREF(func);
Brett Cannon044283a2016-07-15 10:41:49 -070013628 if (NULL == path_repr) {
13629 return NULL;
13630 }
13631
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013632 if (!(PyUnicode_Check(path_repr) || PyBytes_Check(path_repr))) {
13633 PyErr_Format(PyExc_TypeError,
13634 "expected %.200s.__fspath__() to return str or bytes, "
Eddie Elizondob3966632019-11-05 07:16:14 -080013635 "not %.200s", _PyType_Name(Py_TYPE(path)),
13636 _PyType_Name(Py_TYPE(path_repr)));
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013637 Py_DECREF(path_repr);
13638 return NULL;
13639 }
13640
Ethan Furman410ef8e2016-06-04 12:06:26 -070013641 return path_repr;
13642}
13643
13644/*[clinic input]
13645os.fspath
13646
13647 path: object
13648
13649Return the file system path representation of the object.
13650
Brett Cannonb4f43e92016-06-09 14:32:08 -070013651If the object is str or bytes, then allow it to pass through as-is. If the
13652object defines __fspath__(), then return the result of that method. All other
13653types raise a TypeError.
Ethan Furman410ef8e2016-06-04 12:06:26 -070013654[clinic start generated code]*/
13655
13656static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030013657os_fspath_impl(PyObject *module, PyObject *path)
13658/*[clinic end generated code: output=c3c3b78ecff2914f input=e357165f7b22490f]*/
Ethan Furman410ef8e2016-06-04 12:06:26 -070013659{
13660 return PyOS_FSPath(path);
13661}
Victor Stinner6036e442015-03-08 01:58:04 +010013662
Victor Stinner9b1f4742016-09-06 16:18:52 -070013663#ifdef HAVE_GETRANDOM_SYSCALL
13664/*[clinic input]
13665os.getrandom
13666
13667 size: Py_ssize_t
13668 flags: int=0
13669
13670Obtain a series of random bytes.
13671[clinic start generated code]*/
13672
13673static PyObject *
13674os_getrandom_impl(PyObject *module, Py_ssize_t size, int flags)
13675/*[clinic end generated code: output=b3a618196a61409c input=59bafac39c594947]*/
13676{
Victor Stinner9b1f4742016-09-06 16:18:52 -070013677 PyObject *bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013678 Py_ssize_t n;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013679
13680 if (size < 0) {
13681 errno = EINVAL;
13682 return posix_error();
13683 }
13684
Victor Stinnerec2319c2016-09-20 23:00:59 +020013685 bytes = PyBytes_FromStringAndSize(NULL, size);
13686 if (bytes == NULL) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013687 PyErr_NoMemory();
13688 return NULL;
13689 }
13690
13691 while (1) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013692 n = syscall(SYS_getrandom,
13693 PyBytes_AS_STRING(bytes),
13694 PyBytes_GET_SIZE(bytes),
13695 flags);
Victor Stinner9b1f4742016-09-06 16:18:52 -070013696 if (n < 0 && errno == EINTR) {
13697 if (PyErr_CheckSignals() < 0) {
Victor Stinnerec2319c2016-09-20 23:00:59 +020013698 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013699 }
Victor Stinnerec2319c2016-09-20 23:00:59 +020013700
13701 /* getrandom() was interrupted by a signal: retry */
Victor Stinner9b1f4742016-09-06 16:18:52 -070013702 continue;
13703 }
13704 break;
13705 }
13706
13707 if (n < 0) {
Victor Stinner9b1f4742016-09-06 16:18:52 -070013708 PyErr_SetFromErrno(PyExc_OSError);
Victor Stinnerec2319c2016-09-20 23:00:59 +020013709 goto error;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013710 }
13711
Victor Stinnerec2319c2016-09-20 23:00:59 +020013712 if (n != size) {
13713 _PyBytes_Resize(&bytes, n);
13714 }
Victor Stinner9b1f4742016-09-06 16:18:52 -070013715
13716 return bytes;
Victor Stinnerec2319c2016-09-20 23:00:59 +020013717
13718error:
13719 Py_DECREF(bytes);
13720 return NULL;
Victor Stinner9b1f4742016-09-06 16:18:52 -070013721}
13722#endif /* HAVE_GETRANDOM_SYSCALL */
13723
Steve Dower2438cdf2019-03-29 16:37:16 -070013724#ifdef MS_WINDOWS
13725/* bpo-36085: Helper functions for managing DLL search directories
13726 * on win32
13727 */
13728
13729typedef DLL_DIRECTORY_COOKIE (WINAPI *PAddDllDirectory)(PCWSTR newDirectory);
13730typedef BOOL (WINAPI *PRemoveDllDirectory)(DLL_DIRECTORY_COOKIE cookie);
13731
13732/*[clinic input]
13733os._add_dll_directory
13734
13735 path: path_t
13736
13737Add a path to the DLL search path.
13738
13739This search path is used when resolving dependencies for imported
13740extension modules (the module itself is resolved through sys.path),
13741and also by ctypes.
13742
13743Returns an opaque value that may be passed to os.remove_dll_directory
13744to remove this directory from the search path.
13745[clinic start generated code]*/
13746
13747static PyObject *
13748os__add_dll_directory_impl(PyObject *module, path_t *path)
13749/*[clinic end generated code: output=80b025daebb5d683 input=1de3e6c13a5808c8]*/
13750{
13751 HMODULE hKernel32;
13752 PAddDllDirectory AddDllDirectory;
13753 DLL_DIRECTORY_COOKIE cookie = 0;
13754 DWORD err = 0;
13755
Saiyang Gou7514f4f2020-02-12 23:47:42 -080013756 if (PySys_Audit("os.add_dll_directory", "(O)", path->object) < 0) {
13757 return NULL;
13758 }
13759
Steve Dower2438cdf2019-03-29 16:37:16 -070013760 /* For Windows 7, we have to load this. As this will be a fairly
13761 infrequent operation, just do it each time. Kernel32 is always
13762 loaded. */
13763 Py_BEGIN_ALLOW_THREADS
13764 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13765 !(AddDllDirectory = (PAddDllDirectory)GetProcAddress(
13766 hKernel32, "AddDllDirectory")) ||
13767 !(cookie = (*AddDllDirectory)(path->wide))) {
13768 err = GetLastError();
13769 }
13770 Py_END_ALLOW_THREADS
13771
13772 if (err) {
13773 return win32_error_object_err("add_dll_directory",
13774 path->object, err);
13775 }
13776
13777 return PyCapsule_New(cookie, "DLL directory cookie", NULL);
13778}
13779
13780/*[clinic input]
13781os._remove_dll_directory
13782
13783 cookie: object
13784
13785Removes a path from the DLL search path.
13786
13787The parameter is an opaque value that was returned from
13788os.add_dll_directory. You can only remove directories that you added
13789yourself.
13790[clinic start generated code]*/
13791
13792static PyObject *
13793os__remove_dll_directory_impl(PyObject *module, PyObject *cookie)
13794/*[clinic end generated code: output=594350433ae535bc input=c1d16a7e7d9dc5dc]*/
13795{
13796 HMODULE hKernel32;
13797 PRemoveDllDirectory RemoveDllDirectory;
13798 DLL_DIRECTORY_COOKIE cookieValue;
13799 DWORD err = 0;
13800
13801 if (!PyCapsule_IsValid(cookie, "DLL directory cookie")) {
13802 PyErr_SetString(PyExc_TypeError,
13803 "Provided cookie was not returned from os.add_dll_directory");
13804 return NULL;
13805 }
13806
13807 cookieValue = (DLL_DIRECTORY_COOKIE)PyCapsule_GetPointer(
13808 cookie, "DLL directory cookie");
13809
13810 /* For Windows 7, we have to load this. As this will be a fairly
13811 infrequent operation, just do it each time. Kernel32 is always
13812 loaded. */
13813 Py_BEGIN_ALLOW_THREADS
13814 if (!(hKernel32 = GetModuleHandleW(L"kernel32")) ||
13815 !(RemoveDllDirectory = (PRemoveDllDirectory)GetProcAddress(
13816 hKernel32, "RemoveDllDirectory")) ||
13817 !(*RemoveDllDirectory)(cookieValue)) {
13818 err = GetLastError();
13819 }
13820 Py_END_ALLOW_THREADS
13821
13822 if (err) {
13823 return win32_error_object_err("remove_dll_directory",
13824 NULL, err);
13825 }
13826
13827 if (PyCapsule_SetName(cookie, NULL)) {
13828 return NULL;
13829 }
13830
13831 Py_RETURN_NONE;
13832}
13833
13834#endif
Larry Hastings31826802013-10-19 00:09:25 -070013835
Victor Stinner65a796e2020-04-01 18:49:29 +020013836
13837/* Only check if WIFEXITED is available: expect that it comes
13838 with WEXITSTATUS, WIFSIGNALED, etc.
13839
13840 os.waitstatus_to_exitcode() is implemented in C and not in Python, so
13841 subprocess can safely call it during late Python finalization without
13842 risking that used os attributes were set to None by _PyImport_Cleanup(). */
13843#if defined(WIFEXITED) || defined(MS_WINDOWS)
13844/*[clinic input]
13845os.waitstatus_to_exitcode
13846
Victor Stinner9bee32b2020-04-22 16:30:35 +020013847 status as status_obj: object
Victor Stinner65a796e2020-04-01 18:49:29 +020013848
13849Convert a wait status to an exit code.
13850
13851On Unix:
13852
13853* If WIFEXITED(status) is true, return WEXITSTATUS(status).
13854* If WIFSIGNALED(status) is true, return -WTERMSIG(status).
13855* Otherwise, raise a ValueError.
13856
13857On Windows, return status shifted right by 8 bits.
13858
13859On Unix, if the process is being traced or if waitpid() was called with
13860WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true.
13861This function must not be called if WIFSTOPPED(status) is true.
13862[clinic start generated code]*/
13863
13864static PyObject *
Victor Stinner9bee32b2020-04-22 16:30:35 +020013865os_waitstatus_to_exitcode_impl(PyObject *module, PyObject *status_obj)
13866/*[clinic end generated code: output=db50b1b0ba3c7153 input=7fe2d7fdaea3db42]*/
Victor Stinner65a796e2020-04-01 18:49:29 +020013867{
Victor Stinner9bee32b2020-04-22 16:30:35 +020013868 if (PyFloat_Check(status_obj)) {
13869 PyErr_SetString(PyExc_TypeError,
13870 "integer argument expected, got float" );
13871 return NULL;
13872 }
Victor Stinner65a796e2020-04-01 18:49:29 +020013873#ifndef MS_WINDOWS
Victor Stinner9bee32b2020-04-22 16:30:35 +020013874 int status = _PyLong_AsInt(status_obj);
13875 if (status == -1 && PyErr_Occurred()) {
13876 return NULL;
13877 }
13878
Victor Stinner65a796e2020-04-01 18:49:29 +020013879 WAIT_TYPE wait_status;
13880 WAIT_STATUS_INT(wait_status) = status;
13881 int exitcode;
13882 if (WIFEXITED(wait_status)) {
13883 exitcode = WEXITSTATUS(wait_status);
13884 /* Sanity check to provide warranty on the function behavior.
13885 It should not occur in practice */
13886 if (exitcode < 0) {
13887 PyErr_Format(PyExc_ValueError, "invalid WEXITSTATUS: %i", exitcode);
13888 return NULL;
13889 }
13890 }
13891 else if (WIFSIGNALED(wait_status)) {
13892 int signum = WTERMSIG(wait_status);
13893 /* Sanity check to provide warranty on the function behavior.
13894 It should not occurs in practice */
13895 if (signum <= 0) {
13896 PyErr_Format(PyExc_ValueError, "invalid WTERMSIG: %i", signum);
13897 return NULL;
13898 }
13899 exitcode = -signum;
13900 } else if (WIFSTOPPED(wait_status)) {
13901 /* Status only received if the process is being traced
13902 or if waitpid() was called with WUNTRACED option. */
13903 int signum = WSTOPSIG(wait_status);
13904 PyErr_Format(PyExc_ValueError,
13905 "process stopped by delivery of signal %i",
13906 signum);
13907 return NULL;
13908 }
13909 else {
13910 PyErr_Format(PyExc_ValueError, "invalid wait status: %i", status);
13911 return NULL;
13912 }
13913 return PyLong_FromLong(exitcode);
13914#else
13915 /* Windows implementation: see os.waitpid() implementation
13916 which uses _cwait(). */
Victor Stinner9bee32b2020-04-22 16:30:35 +020013917 unsigned long long status = PyLong_AsUnsignedLongLong(status_obj);
13918 if (status == (unsigned long long)-1 && PyErr_Occurred()) {
13919 return NULL;
13920 }
13921
13922 unsigned long long exitcode = (status >> 8);
13923 /* ExitProcess() accepts an UINT type:
13924 reject exit code which doesn't fit in an UINT */
13925 if (exitcode > UINT_MAX) {
13926 PyErr_Format(PyExc_ValueError, "invalid exit code: %llu", exitcode);
13927 return NULL;
13928 }
13929 return PyLong_FromUnsignedLong((unsigned long)exitcode);
Victor Stinner65a796e2020-04-01 18:49:29 +020013930#endif
13931}
13932#endif
13933
13934
Fred Drake5ab8eaf1999-12-09 21:13:07 +000013935static PyMethodDef posix_methods[] = {
Larry Hastings31826802013-10-19 00:09:25 -070013936
13937 OS_STAT_METHODDEF
13938 OS_ACCESS_METHODDEF
13939 OS_TTYNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013940 OS_CHDIR_METHODDEF
13941 OS_CHFLAGS_METHODDEF
13942 OS_CHMOD_METHODDEF
13943 OS_FCHMOD_METHODDEF
13944 OS_LCHMOD_METHODDEF
13945 OS_CHOWN_METHODDEF
13946 OS_FCHOWN_METHODDEF
13947 OS_LCHOWN_METHODDEF
13948 OS_LCHFLAGS_METHODDEF
13949 OS_CHROOT_METHODDEF
13950 OS_CTERMID_METHODDEF
13951 OS_GETCWD_METHODDEF
13952 OS_GETCWDB_METHODDEF
13953 OS_LINK_METHODDEF
13954 OS_LISTDIR_METHODDEF
13955 OS_LSTAT_METHODDEF
13956 OS_MKDIR_METHODDEF
13957 OS_NICE_METHODDEF
13958 OS_GETPRIORITY_METHODDEF
13959 OS_SETPRIORITY_METHODDEF
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000013960 OS_POSIX_SPAWN_METHODDEF
Joannah Nanjekye92b83222019-01-16 16:29:26 +030013961 OS_POSIX_SPAWNP_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030013962 OS_READLINK_METHODDEF
Pablo Galindoaac4d032019-05-31 19:39:47 +010013963 OS_COPY_FILE_RANGE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013964 OS_RENAME_METHODDEF
13965 OS_REPLACE_METHODDEF
13966 OS_RMDIR_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013967 OS_SYMLINK_METHODDEF
13968 OS_SYSTEM_METHODDEF
13969 OS_UMASK_METHODDEF
13970 OS_UNAME_METHODDEF
13971 OS_UNLINK_METHODDEF
13972 OS_REMOVE_METHODDEF
13973 OS_UTIME_METHODDEF
13974 OS_TIMES_METHODDEF
13975 OS__EXIT_METHODDEF
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020013976 OS__FCOPYFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013977 OS_EXECV_METHODDEF
13978 OS_EXECVE_METHODDEF
13979 OS_SPAWNV_METHODDEF
13980 OS_SPAWNVE_METHODDEF
13981 OS_FORK1_METHODDEF
13982 OS_FORK_METHODDEF
Antoine Pitrou346cbd32017-05-27 17:50:54 +020013983 OS_REGISTER_AT_FORK_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100013984 OS_SCHED_GET_PRIORITY_MAX_METHODDEF
13985 OS_SCHED_GET_PRIORITY_MIN_METHODDEF
13986 OS_SCHED_GETPARAM_METHODDEF
13987 OS_SCHED_GETSCHEDULER_METHODDEF
13988 OS_SCHED_RR_GET_INTERVAL_METHODDEF
13989 OS_SCHED_SETPARAM_METHODDEF
13990 OS_SCHED_SETSCHEDULER_METHODDEF
13991 OS_SCHED_YIELD_METHODDEF
13992 OS_SCHED_SETAFFINITY_METHODDEF
13993 OS_SCHED_GETAFFINITY_METHODDEF
13994 OS_OPENPTY_METHODDEF
13995 OS_FORKPTY_METHODDEF
13996 OS_GETEGID_METHODDEF
13997 OS_GETEUID_METHODDEF
13998 OS_GETGID_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030013999 OS_GETGROUPLIST_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014000 OS_GETGROUPS_METHODDEF
14001 OS_GETPID_METHODDEF
14002 OS_GETPGRP_METHODDEF
14003 OS_GETPPID_METHODDEF
14004 OS_GETUID_METHODDEF
14005 OS_GETLOGIN_METHODDEF
14006 OS_KILL_METHODDEF
14007 OS_KILLPG_METHODDEF
14008 OS_PLOCK_METHODDEF
Steve Dowercc16be82016-09-08 10:35:16 -070014009 OS_STARTFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014010 OS_SETUID_METHODDEF
14011 OS_SETEUID_METHODDEF
14012 OS_SETREUID_METHODDEF
14013 OS_SETGID_METHODDEF
14014 OS_SETEGID_METHODDEF
14015 OS_SETREGID_METHODDEF
14016 OS_SETGROUPS_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014017 OS_INITGROUPS_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014018 OS_GETPGID_METHODDEF
14019 OS_SETPGRP_METHODDEF
14020 OS_WAIT_METHODDEF
14021 OS_WAIT3_METHODDEF
14022 OS_WAIT4_METHODDEF
14023 OS_WAITID_METHODDEF
14024 OS_WAITPID_METHODDEF
Benjamin Peterson6c4c45e2019-11-05 19:21:29 -080014025 OS_PIDFD_OPEN_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014026 OS_GETSID_METHODDEF
14027 OS_SETSID_METHODDEF
14028 OS_SETPGID_METHODDEF
14029 OS_TCGETPGRP_METHODDEF
14030 OS_TCSETPGRP_METHODDEF
14031 OS_OPEN_METHODDEF
14032 OS_CLOSE_METHODDEF
14033 OS_CLOSERANGE_METHODDEF
14034 OS_DEVICE_ENCODING_METHODDEF
14035 OS_DUP_METHODDEF
14036 OS_DUP2_METHODDEF
14037 OS_LOCKF_METHODDEF
14038 OS_LSEEK_METHODDEF
14039 OS_READ_METHODDEF
14040 OS_READV_METHODDEF
14041 OS_PREAD_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014042 OS_PREADV_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014043 OS_WRITE_METHODDEF
14044 OS_WRITEV_METHODDEF
14045 OS_PWRITE_METHODDEF
Pablo Galindo4defba32018-01-27 16:16:37 +000014046 OS_PWRITEV_METHODDEF
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014047 OS_SENDFILE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014048 OS_FSTAT_METHODDEF
14049 OS_ISATTY_METHODDEF
14050 OS_PIPE_METHODDEF
14051 OS_PIPE2_METHODDEF
14052 OS_MKFIFO_METHODDEF
14053 OS_MKNOD_METHODDEF
14054 OS_MAJOR_METHODDEF
14055 OS_MINOR_METHODDEF
14056 OS_MAKEDEV_METHODDEF
14057 OS_FTRUNCATE_METHODDEF
14058 OS_TRUNCATE_METHODDEF
14059 OS_POSIX_FALLOCATE_METHODDEF
14060 OS_POSIX_FADVISE_METHODDEF
14061 OS_PUTENV_METHODDEF
14062 OS_UNSETENV_METHODDEF
14063 OS_STRERROR_METHODDEF
14064 OS_FCHDIR_METHODDEF
14065 OS_FSYNC_METHODDEF
14066 OS_SYNC_METHODDEF
14067 OS_FDATASYNC_METHODDEF
14068 OS_WCOREDUMP_METHODDEF
14069 OS_WIFCONTINUED_METHODDEF
14070 OS_WIFSTOPPED_METHODDEF
14071 OS_WIFSIGNALED_METHODDEF
14072 OS_WIFEXITED_METHODDEF
14073 OS_WEXITSTATUS_METHODDEF
14074 OS_WTERMSIG_METHODDEF
14075 OS_WSTOPSIG_METHODDEF
14076 OS_FSTATVFS_METHODDEF
14077 OS_STATVFS_METHODDEF
14078 OS_CONFSTR_METHODDEF
14079 OS_SYSCONF_METHODDEF
14080 OS_FPATHCONF_METHODDEF
14081 OS_PATHCONF_METHODDEF
14082 OS_ABORT_METHODDEF
Serhiy Storchakaf0b50152015-05-13 00:52:39 +030014083 OS__GETFULLPATHNAME_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014084 OS__GETDISKUSAGE_METHODDEF
14085 OS__GETFINALPATHNAME_METHODDEF
14086 OS__GETVOLUMEPATHNAME_METHODDEF
14087 OS_GETLOADAVG_METHODDEF
14088 OS_URANDOM_METHODDEF
14089 OS_SETRESUID_METHODDEF
14090 OS_SETRESGID_METHODDEF
14091 OS_GETRESUID_METHODDEF
14092 OS_GETRESGID_METHODDEF
Martin v. Löwis7aed61a2009-11-27 14:09:49 +000014093
Larry Hastings2f936352014-08-05 14:04:04 +100014094 OS_GETXATTR_METHODDEF
14095 OS_SETXATTR_METHODDEF
14096 OS_REMOVEXATTR_METHODDEF
14097 OS_LISTXATTR_METHODDEF
14098
Serhiy Storchaka2b560312020-04-18 19:14:10 +030014099 OS_GET_TERMINAL_SIZE_METHODDEF
Larry Hastings2f936352014-08-05 14:04:04 +100014100 OS_CPU_COUNT_METHODDEF
14101 OS_GET_INHERITABLE_METHODDEF
14102 OS_SET_INHERITABLE_METHODDEF
14103 OS_GET_HANDLE_INHERITABLE_METHODDEF
14104 OS_SET_HANDLE_INHERITABLE_METHODDEF
Serhiy Storchaka12a69db2018-09-17 15:38:27 +030014105 OS_GET_BLOCKING_METHODDEF
14106 OS_SET_BLOCKING_METHODDEF
Serhiy Storchaka49d02d12016-11-06 13:45:33 +020014107 OS_SCANDIR_METHODDEF
Ethan Furman410ef8e2016-06-04 12:06:26 -070014108 OS_FSPATH_METHODDEF
Victor Stinner9b1f4742016-09-06 16:18:52 -070014109 OS_GETRANDOM_METHODDEF
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014110 OS_MEMFD_CREATE_METHODDEF
Steve Dower2438cdf2019-03-29 16:37:16 -070014111 OS__ADD_DLL_DIRECTORY_METHODDEF
14112 OS__REMOVE_DLL_DIRECTORY_METHODDEF
Victor Stinner65a796e2020-04-01 18:49:29 +020014113 OS_WAITSTATUS_TO_EXITCODE_METHODDEF
Victor Stinner8c62be82010-05-06 00:08:46 +000014114 {NULL, NULL} /* Sentinel */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014115};
14116
Barry Warsaw4a342091996-12-19 23:50:02 +000014117static int
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014118all_ins(PyObject *m)
Barry Warsaw4a342091996-12-19 23:50:02 +000014119{
Guido van Rossum94f6f721999-01-06 18:42:14 +000014120#ifdef F_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014121 if (PyModule_AddIntMacro(m, F_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014122#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014123#ifdef R_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014124 if (PyModule_AddIntMacro(m, R_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014125#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014126#ifdef W_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014127 if (PyModule_AddIntMacro(m, W_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014128#endif
Guido van Rossum94f6f721999-01-06 18:42:14 +000014129#ifdef X_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014130 if (PyModule_AddIntMacro(m, X_OK)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014131#endif
Fred Drakec9680921999-12-13 16:37:25 +000014132#ifdef NGROUPS_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014133 if (PyModule_AddIntMacro(m, NGROUPS_MAX)) return -1;
Fred Drakec9680921999-12-13 16:37:25 +000014134#endif
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014135#ifdef TMP_MAX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014136 if (PyModule_AddIntMacro(m, TMP_MAX)) return -1;
Fred Drake5ab8eaf1999-12-09 21:13:07 +000014137#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014138#ifdef WCONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014139 if (PyModule_AddIntMacro(m, WCONTINUED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014140#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014141#ifdef WNOHANG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014142 if (PyModule_AddIntMacro(m, WNOHANG)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014143#endif
Fred Drake106c1a02002-04-23 15:58:02 +000014144#ifdef WUNTRACED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014145 if (PyModule_AddIntMacro(m, WUNTRACED)) return -1;
Fred Drake106c1a02002-04-23 15:58:02 +000014146#endif
Barry Warsaw4a342091996-12-19 23:50:02 +000014147#ifdef O_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014148 if (PyModule_AddIntMacro(m, O_RDONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014149#endif
14150#ifdef O_WRONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014151 if (PyModule_AddIntMacro(m, O_WRONLY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014152#endif
14153#ifdef O_RDWR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014154 if (PyModule_AddIntMacro(m, O_RDWR)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014155#endif
14156#ifdef O_NDELAY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014157 if (PyModule_AddIntMacro(m, O_NDELAY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014158#endif
14159#ifdef O_NONBLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014160 if (PyModule_AddIntMacro(m, O_NONBLOCK)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014161#endif
14162#ifdef O_APPEND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014163 if (PyModule_AddIntMacro(m, O_APPEND)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014164#endif
14165#ifdef O_DSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014166 if (PyModule_AddIntMacro(m, O_DSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014167#endif
14168#ifdef O_RSYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014169 if (PyModule_AddIntMacro(m, O_RSYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014170#endif
14171#ifdef O_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014172 if (PyModule_AddIntMacro(m, O_SYNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014173#endif
14174#ifdef O_NOCTTY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014175 if (PyModule_AddIntMacro(m, O_NOCTTY)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014176#endif
14177#ifdef O_CREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014178 if (PyModule_AddIntMacro(m, O_CREAT)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014179#endif
14180#ifdef O_EXCL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014181 if (PyModule_AddIntMacro(m, O_EXCL)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014182#endif
14183#ifdef O_TRUNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014184 if (PyModule_AddIntMacro(m, O_TRUNC)) return -1;
Barry Warsaw4a342091996-12-19 23:50:02 +000014185#endif
Guido van Rossum98d9d091997-08-08 21:48:51 +000014186#ifdef O_BINARY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014187 if (PyModule_AddIntMacro(m, O_BINARY)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014188#endif
14189#ifdef O_TEXT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014190 if (PyModule_AddIntMacro(m, O_TEXT)) return -1;
Guido van Rossum98d9d091997-08-08 21:48:51 +000014191#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014192#ifdef O_XATTR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014193 if (PyModule_AddIntMacro(m, O_XATTR)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014194#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014195#ifdef O_LARGEFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014196 if (PyModule_AddIntMacro(m, O_LARGEFILE)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014197#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014198#ifndef __GNU__
Skip Montanaro5ff14922005-05-16 02:42:22 +000014199#ifdef O_SHLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014200 if (PyModule_AddIntMacro(m, O_SHLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014201#endif
14202#ifdef O_EXLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014203 if (PyModule_AddIntMacro(m, O_EXLOCK)) return -1;
Skip Montanaro5ff14922005-05-16 02:42:22 +000014204#endif
doko@ubuntu.comfcff4372016-06-13 16:33:04 +020014205#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014206#ifdef O_EXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014207 if (PyModule_AddIntMacro(m, O_EXEC)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014208#endif
14209#ifdef O_SEARCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014210 if (PyModule_AddIntMacro(m, O_SEARCH)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014211#endif
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014212#ifdef O_PATH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014213 if (PyModule_AddIntMacro(m, O_PATH)) return -1;
Benjamin Peterson3b965a22013-03-13 10:27:41 -050014214#endif
Jesus Ceacf381202012-04-24 20:44:40 +020014215#ifdef O_TTY_INIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014216 if (PyModule_AddIntMacro(m, O_TTY_INIT)) return -1;
Jesus Ceacf381202012-04-24 20:44:40 +020014217#endif
Christian Heimes177b3f92013-08-16 14:35:09 +020014218#ifdef O_TMPFILE
14219 if (PyModule_AddIntMacro(m, O_TMPFILE)) return -1;
14220#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014221#ifdef PRIO_PROCESS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014222 if (PyModule_AddIntMacro(m, PRIO_PROCESS)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014223#endif
14224#ifdef PRIO_PGRP
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014225 if (PyModule_AddIntMacro(m, PRIO_PGRP)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014226#endif
14227#ifdef PRIO_USER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014228 if (PyModule_AddIntMacro(m, PRIO_USER)) return -1;
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014229#endif
Charles-François Natali1e045b12011-05-22 20:42:32 +020014230#ifdef O_CLOEXEC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014231 if (PyModule_AddIntMacro(m, O_CLOEXEC)) return -1;
Charles-François Natali1e045b12011-05-22 20:42:32 +020014232#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014233#ifdef O_ACCMODE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014234 if (PyModule_AddIntMacro(m, O_ACCMODE)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014235#endif
Giampaolo Rodolà18e8bcb2011-02-25 20:57:54 +000014236
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014237
Jesus Cea94363612012-06-22 18:32:07 +020014238#ifdef SEEK_HOLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014239 if (PyModule_AddIntMacro(m, SEEK_HOLE)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014240#endif
14241#ifdef SEEK_DATA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014242 if (PyModule_AddIntMacro(m, SEEK_DATA)) return -1;
Jesus Cea94363612012-06-22 18:32:07 +020014243#endif
14244
Tim Peters5aa91602002-01-30 05:46:57 +000014245/* MS Windows */
14246#ifdef O_NOINHERIT
Victor Stinner8c62be82010-05-06 00:08:46 +000014247 /* Don't inherit in child processes. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014248 if (PyModule_AddIntMacro(m, O_NOINHERIT)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014249#endif
14250#ifdef _O_SHORT_LIVED
Victor Stinner8c62be82010-05-06 00:08:46 +000014251 /* Optimize for short life (keep in memory). */
14252 /* MS forgot to define this one with a non-underscore form too. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014253 if (PyModule_AddIntConstant(m, "O_SHORT_LIVED", _O_SHORT_LIVED)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014254#endif
14255#ifdef O_TEMPORARY
Victor Stinner8c62be82010-05-06 00:08:46 +000014256 /* Automatically delete when last handle is closed. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014257 if (PyModule_AddIntMacro(m, O_TEMPORARY)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014258#endif
14259#ifdef O_RANDOM
Victor Stinner8c62be82010-05-06 00:08:46 +000014260 /* Optimize for random access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014261 if (PyModule_AddIntMacro(m, O_RANDOM)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014262#endif
14263#ifdef O_SEQUENTIAL
Victor Stinner8c62be82010-05-06 00:08:46 +000014264 /* Optimize for sequential access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014265 if (PyModule_AddIntMacro(m, O_SEQUENTIAL)) return -1;
Tim Peters5aa91602002-01-30 05:46:57 +000014266#endif
14267
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014268/* GNU extensions. */
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014269#ifdef O_ASYNC
Victor Stinner8c62be82010-05-06 00:08:46 +000014270 /* Send a SIGIO signal whenever input or output
14271 becomes available on file descriptor */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014272 if (PyModule_AddIntMacro(m, O_ASYNC)) return -1;
Alexandre Vassalottibee32532008-05-16 18:15:12 +000014273#endif
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014274#ifdef O_DIRECT
Victor Stinner8c62be82010-05-06 00:08:46 +000014275 /* Direct disk access. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014276 if (PyModule_AddIntMacro(m, O_DIRECT)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014277#endif
14278#ifdef O_DIRECTORY
Victor Stinner8c62be82010-05-06 00:08:46 +000014279 /* Must be a directory. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014280 if (PyModule_AddIntMacro(m, O_DIRECTORY)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014281#endif
14282#ifdef O_NOFOLLOW
Victor Stinner8c62be82010-05-06 00:08:46 +000014283 /* Do not follow links. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014284 if (PyModule_AddIntMacro(m, O_NOFOLLOW)) return -1;
Martin v. Löwis4fe3c272001-10-18 22:05:36 +000014285#endif
Jesus Cea1d642d22012-04-24 20:59:17 +020014286#ifdef O_NOLINKS
14287 /* Fails if link count of the named file is greater than 1 */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014288 if (PyModule_AddIntMacro(m, O_NOLINKS)) return -1;
Jesus Cea1d642d22012-04-24 20:59:17 +020014289#endif
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014290#ifdef O_NOATIME
Victor Stinner8c62be82010-05-06 00:08:46 +000014291 /* Do not update the access time. */
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014292 if (PyModule_AddIntMacro(m, O_NOATIME)) return -1;
Guido van Rossum0d3fb8a2007-11-26 23:23:18 +000014293#endif
Guido van Rossumd48f2521997-12-05 22:19:34 +000014294
Victor Stinner8c62be82010-05-06 00:08:46 +000014295 /* These come from sysexits.h */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014296#ifdef EX_OK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014297 if (PyModule_AddIntMacro(m, EX_OK)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014298#endif /* EX_OK */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014299#ifdef EX_USAGE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014300 if (PyModule_AddIntMacro(m, EX_USAGE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014301#endif /* EX_USAGE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014302#ifdef EX_DATAERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014303 if (PyModule_AddIntMacro(m, EX_DATAERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014304#endif /* EX_DATAERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014305#ifdef EX_NOINPUT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014306 if (PyModule_AddIntMacro(m, EX_NOINPUT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014307#endif /* EX_NOINPUT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014308#ifdef EX_NOUSER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014309 if (PyModule_AddIntMacro(m, EX_NOUSER)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014310#endif /* EX_NOUSER */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014311#ifdef EX_NOHOST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014312 if (PyModule_AddIntMacro(m, EX_NOHOST)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014313#endif /* EX_NOHOST */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014314#ifdef EX_UNAVAILABLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014315 if (PyModule_AddIntMacro(m, EX_UNAVAILABLE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014316#endif /* EX_UNAVAILABLE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014317#ifdef EX_SOFTWARE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014318 if (PyModule_AddIntMacro(m, EX_SOFTWARE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014319#endif /* EX_SOFTWARE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014320#ifdef EX_OSERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014321 if (PyModule_AddIntMacro(m, EX_OSERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014322#endif /* EX_OSERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014323#ifdef EX_OSFILE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014324 if (PyModule_AddIntMacro(m, EX_OSFILE)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014325#endif /* EX_OSFILE */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014326#ifdef EX_CANTCREAT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014327 if (PyModule_AddIntMacro(m, EX_CANTCREAT)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014328#endif /* EX_CANTCREAT */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014329#ifdef EX_IOERR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014330 if (PyModule_AddIntMacro(m, EX_IOERR)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014331#endif /* EX_IOERR */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014332#ifdef EX_TEMPFAIL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014333 if (PyModule_AddIntMacro(m, EX_TEMPFAIL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014334#endif /* EX_TEMPFAIL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014335#ifdef EX_PROTOCOL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014336 if (PyModule_AddIntMacro(m, EX_PROTOCOL)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014337#endif /* EX_PROTOCOL */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014338#ifdef EX_NOPERM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014339 if (PyModule_AddIntMacro(m, EX_NOPERM)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014340#endif /* EX_NOPERM */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014341#ifdef EX_CONFIG
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014342 if (PyModule_AddIntMacro(m, EX_CONFIG)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014343#endif /* EX_CONFIG */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014344#ifdef EX_NOTFOUND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014345 if (PyModule_AddIntMacro(m, EX_NOTFOUND)) return -1;
Neal Norwitz8e914d92003-01-10 15:29:16 +000014346#endif /* EX_NOTFOUND */
Barry Warsaw5676bd12003-01-07 20:57:09 +000014347
Amaury Forgeot d'Arc66d00ad2010-09-10 18:11:45 +000014348 /* statvfs */
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014349#ifdef ST_RDONLY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014350 if (PyModule_AddIntMacro(m, ST_RDONLY)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014351#endif /* ST_RDONLY */
14352#ifdef ST_NOSUID
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014353 if (PyModule_AddIntMacro(m, ST_NOSUID)) return -1;
Andrew M. Kuchling4ea04a32010-08-18 22:30:34 +000014354#endif /* ST_NOSUID */
14355
doko@ubuntu.comca616a22013-12-08 15:23:07 +010014356 /* GNU extensions */
14357#ifdef ST_NODEV
14358 if (PyModule_AddIntMacro(m, ST_NODEV)) return -1;
14359#endif /* ST_NODEV */
14360#ifdef ST_NOEXEC
14361 if (PyModule_AddIntMacro(m, ST_NOEXEC)) return -1;
14362#endif /* ST_NOEXEC */
14363#ifdef ST_SYNCHRONOUS
14364 if (PyModule_AddIntMacro(m, ST_SYNCHRONOUS)) return -1;
14365#endif /* ST_SYNCHRONOUS */
14366#ifdef ST_MANDLOCK
14367 if (PyModule_AddIntMacro(m, ST_MANDLOCK)) return -1;
14368#endif /* ST_MANDLOCK */
14369#ifdef ST_WRITE
14370 if (PyModule_AddIntMacro(m, ST_WRITE)) return -1;
14371#endif /* ST_WRITE */
14372#ifdef ST_APPEND
14373 if (PyModule_AddIntMacro(m, ST_APPEND)) return -1;
14374#endif /* ST_APPEND */
14375#ifdef ST_NOATIME
14376 if (PyModule_AddIntMacro(m, ST_NOATIME)) return -1;
14377#endif /* ST_NOATIME */
14378#ifdef ST_NODIRATIME
14379 if (PyModule_AddIntMacro(m, ST_NODIRATIME)) return -1;
14380#endif /* ST_NODIRATIME */
14381#ifdef ST_RELATIME
14382 if (PyModule_AddIntMacro(m, ST_RELATIME)) return -1;
14383#endif /* ST_RELATIME */
14384
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014385 /* FreeBSD sendfile() constants */
14386#ifdef SF_NODISKIO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014387 if (PyModule_AddIntMacro(m, SF_NODISKIO)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014388#endif
14389#ifdef SF_MNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014390 if (PyModule_AddIntMacro(m, SF_MNOWAIT)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014391#endif
14392#ifdef SF_SYNC
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014393 if (PyModule_AddIntMacro(m, SF_SYNC)) return -1;
Giampaolo Rodolàc9c2c8b2011-02-25 14:39:16 +000014394#endif
14395
Ross Lagerwall7807c352011-03-17 20:20:30 +020014396 /* constants for posix_fadvise */
14397#ifdef POSIX_FADV_NORMAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014398 if (PyModule_AddIntMacro(m, POSIX_FADV_NORMAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014399#endif
14400#ifdef POSIX_FADV_SEQUENTIAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014401 if (PyModule_AddIntMacro(m, POSIX_FADV_SEQUENTIAL)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014402#endif
14403#ifdef POSIX_FADV_RANDOM
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014404 if (PyModule_AddIntMacro(m, POSIX_FADV_RANDOM)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014405#endif
14406#ifdef POSIX_FADV_NOREUSE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014407 if (PyModule_AddIntMacro(m, POSIX_FADV_NOREUSE)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014408#endif
14409#ifdef POSIX_FADV_WILLNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014410 if (PyModule_AddIntMacro(m, POSIX_FADV_WILLNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014411#endif
14412#ifdef POSIX_FADV_DONTNEED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014413 if (PyModule_AddIntMacro(m, POSIX_FADV_DONTNEED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014414#endif
14415
14416 /* constants for waitid */
14417#if defined(HAVE_SYS_WAIT_H) && defined(HAVE_WAITID)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014418 if (PyModule_AddIntMacro(m, P_PID)) return -1;
14419 if (PyModule_AddIntMacro(m, P_PGID)) return -1;
14420 if (PyModule_AddIntMacro(m, P_ALL)) return -1;
Benjamin Peterson5c0c3252019-11-05 21:58:31 -080014421#ifdef P_PIDFD
14422 if (PyModule_AddIntMacro(m, P_PIDFD)) return -1;
14423#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014424#endif
14425#ifdef WEXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014426 if (PyModule_AddIntMacro(m, WEXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014427#endif
14428#ifdef WNOWAIT
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014429 if (PyModule_AddIntMacro(m, WNOWAIT)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014430#endif
14431#ifdef WSTOPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014432 if (PyModule_AddIntMacro(m, WSTOPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014433#endif
14434#ifdef CLD_EXITED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014435 if (PyModule_AddIntMacro(m, CLD_EXITED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014436#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014437#ifdef CLD_KILLED
14438 if (PyModule_AddIntMacro(m, CLD_KILLED)) return -1;
14439#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014440#ifdef CLD_DUMPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014441 if (PyModule_AddIntMacro(m, CLD_DUMPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014442#endif
14443#ifdef CLD_TRAPPED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014444 if (PyModule_AddIntMacro(m, CLD_TRAPPED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014445#endif
Dong-hee Na2eba6ad2019-10-21 16:01:05 +090014446#ifdef CLD_STOPPED
14447 if (PyModule_AddIntMacro(m, CLD_STOPPED)) return -1;
14448#endif
Ross Lagerwall7807c352011-03-17 20:20:30 +020014449#ifdef CLD_CONTINUED
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014450 if (PyModule_AddIntMacro(m, CLD_CONTINUED)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014451#endif
14452
14453 /* constants for lockf */
14454#ifdef F_LOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014455 if (PyModule_AddIntMacro(m, F_LOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014456#endif
14457#ifdef F_TLOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014458 if (PyModule_AddIntMacro(m, F_TLOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014459#endif
14460#ifdef F_ULOCK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014461 if (PyModule_AddIntMacro(m, F_ULOCK)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014462#endif
14463#ifdef F_TEST
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014464 if (PyModule_AddIntMacro(m, F_TEST)) return -1;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014465#endif
14466
Pablo Galindo4defba32018-01-27 16:16:37 +000014467#ifdef RWF_DSYNC
14468 if (PyModule_AddIntConstant(m, "RWF_DSYNC", RWF_DSYNC)) return -1;
14469#endif
14470#ifdef RWF_HIPRI
14471 if (PyModule_AddIntConstant(m, "RWF_HIPRI", RWF_HIPRI)) return -1;
14472#endif
14473#ifdef RWF_SYNC
14474 if (PyModule_AddIntConstant(m, "RWF_SYNC", RWF_SYNC)) return -1;
14475#endif
14476#ifdef RWF_NOWAIT
14477 if (PyModule_AddIntConstant(m, "RWF_NOWAIT", RWF_NOWAIT)) return -1;
14478#endif
14479
Pablo Galindo6c6ddf92018-01-29 01:56:10 +000014480/* constants for posix_spawn */
14481#ifdef HAVE_POSIX_SPAWN
14482 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_OPEN", POSIX_SPAWN_OPEN)) return -1;
14483 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_CLOSE", POSIX_SPAWN_CLOSE)) return -1;
14484 if (PyModule_AddIntConstant(m, "POSIX_SPAWN_DUP2", POSIX_SPAWN_DUP2)) return -1;
14485#endif
14486
pxinwrf2d7ac72019-05-21 18:46:37 +080014487#if defined(HAVE_SPAWNV) || defined (HAVE_RTPSPAWN)
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014488 if (PyModule_AddIntConstant(m, "P_WAIT", _P_WAIT)) return -1;
14489 if (PyModule_AddIntConstant(m, "P_NOWAIT", _P_NOWAIT)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014490 if (PyModule_AddIntConstant(m, "P_NOWAITO", _P_NOWAITO)) return -1;
pxinwrf2d7ac72019-05-21 18:46:37 +080014491#endif
14492#ifdef HAVE_SPAWNV
14493 if (PyModule_AddIntConstant(m, "P_OVERLAY", _OLD_P_OVERLAY)) return -1;
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014494 if (PyModule_AddIntConstant(m, "P_DETACH", _P_DETACH)) return -1;
Guido van Rossum246bc171999-02-01 23:54:31 +000014495#endif
14496
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014497#ifdef HAVE_SCHED_H
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014498#ifdef SCHED_OTHER
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014499 if (PyModule_AddIntMacro(m, SCHED_OTHER)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014500#endif
14501#ifdef SCHED_FIFO
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014502 if (PyModule_AddIntMacro(m, SCHED_FIFO)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014503#endif
14504#ifdef SCHED_RR
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014505 if (PyModule_AddIntMacro(m, SCHED_RR)) return -1;
Benjamin Petersondbaa5592016-07-30 23:21:50 -070014506#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014507#ifdef SCHED_SPORADIC
messi Liao0d322182017-06-13 22:30:43 +080014508 if (PyModule_AddIntMacro(m, SCHED_SPORADIC)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014509#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014510#ifdef SCHED_BATCH
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014511 if (PyModule_AddIntMacro(m, SCHED_BATCH)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014512#endif
14513#ifdef SCHED_IDLE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014514 if (PyModule_AddIntMacro(m, SCHED_IDLE)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014515#endif
14516#ifdef SCHED_RESET_ON_FORK
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014517 if (PyModule_AddIntMacro(m, SCHED_RESET_ON_FORK)) return -1;
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014518#endif
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014519#ifdef SCHED_SYS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014520 if (PyModule_AddIntMacro(m, SCHED_SYS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014521#endif
14522#ifdef SCHED_IA
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014523 if (PyModule_AddIntMacro(m, SCHED_IA)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014524#endif
14525#ifdef SCHED_FSS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014526 if (PyModule_AddIntMacro(m, SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014527#endif
14528#ifdef SCHED_FX
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014529 if (PyModule_AddIntConstant(m, "SCHED_FX", SCHED_FSS)) return -1;
Jesus Ceaf2cb4e82011-09-09 23:55:42 +020014530#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014531#endif
14532
Benjamin Peterson9428d532011-09-14 11:45:52 -040014533#ifdef USE_XATTRS
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014534 if (PyModule_AddIntMacro(m, XATTR_CREATE)) return -1;
14535 if (PyModule_AddIntMacro(m, XATTR_REPLACE)) return -1;
14536 if (PyModule_AddIntMacro(m, XATTR_SIZE_MAX)) return -1;
Benjamin Peterson799bd802011-08-31 22:15:17 -040014537#endif
14538
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014539#if HAVE_DECL_RTLD_LAZY
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014540 if (PyModule_AddIntMacro(m, RTLD_LAZY)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014541#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014542#if HAVE_DECL_RTLD_NOW
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014543 if (PyModule_AddIntMacro(m, RTLD_NOW)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014544#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014545#if HAVE_DECL_RTLD_GLOBAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014546 if (PyModule_AddIntMacro(m, RTLD_GLOBAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014547#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014548#if HAVE_DECL_RTLD_LOCAL
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014549 if (PyModule_AddIntMacro(m, RTLD_LOCAL)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014550#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014551#if HAVE_DECL_RTLD_NODELETE
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014552 if (PyModule_AddIntMacro(m, RTLD_NODELETE)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014553#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014554#if HAVE_DECL_RTLD_NOLOAD
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014555 if (PyModule_AddIntMacro(m, RTLD_NOLOAD)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014556#endif
Serhiy Storchakac2f7d872016-05-04 09:44:44 +030014557#if HAVE_DECL_RTLD_DEEPBIND
Charles-Francois Natali74ca8862013-05-20 19:13:19 +020014558 if (PyModule_AddIntMacro(m, RTLD_DEEPBIND)) return -1;
Victor Stinner8b905bd2011-10-25 13:34:04 +020014559#endif
Michael Feltc5ae1692017-12-19 13:58:49 +010014560#if HAVE_DECL_RTLD_MEMBER
14561 if (PyModule_AddIntMacro(m, RTLD_MEMBER)) return -1;
14562#endif
Victor Stinner8b905bd2011-10-25 13:34:04 +020014563
Victor Stinner9b1f4742016-09-06 16:18:52 -070014564#ifdef HAVE_GETRANDOM_SYSCALL
14565 if (PyModule_AddIntMacro(m, GRND_RANDOM)) return -1;
14566 if (PyModule_AddIntMacro(m, GRND_NONBLOCK)) return -1;
14567#endif
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014568#ifdef HAVE_MEMFD_CREATE
14569 if (PyModule_AddIntMacro(m, MFD_CLOEXEC)) return -1;
14570 if (PyModule_AddIntMacro(m, MFD_ALLOW_SEALING)) return -1;
14571#ifdef MFD_HUGETLB
14572 if (PyModule_AddIntMacro(m, MFD_HUGETLB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014573#endif
14574#ifdef MFD_HUGE_SHIFT
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014575 if (PyModule_AddIntMacro(m, MFD_HUGE_SHIFT)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014576#endif
14577#ifdef MFD_HUGE_MASK
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014578 if (PyModule_AddIntMacro(m, MFD_HUGE_MASK)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014579#endif
14580#ifdef MFD_HUGE_64KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014581 if (PyModule_AddIntMacro(m, MFD_HUGE_64KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014582#endif
14583#ifdef MFD_HUGE_512KB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014584 if (PyModule_AddIntMacro(m, MFD_HUGE_512KB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014585#endif
14586#ifdef MFD_HUGE_1MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014587 if (PyModule_AddIntMacro(m, MFD_HUGE_1MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014588#endif
14589#ifdef MFD_HUGE_2MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014590 if (PyModule_AddIntMacro(m, MFD_HUGE_2MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014591#endif
14592#ifdef MFD_HUGE_8MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014593 if (PyModule_AddIntMacro(m, MFD_HUGE_8MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014594#endif
14595#ifdef MFD_HUGE_16MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014596 if (PyModule_AddIntMacro(m, MFD_HUGE_16MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014597#endif
14598#ifdef MFD_HUGE_32MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014599 if (PyModule_AddIntMacro(m, MFD_HUGE_32MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014600#endif
14601#ifdef MFD_HUGE_256MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014602 if (PyModule_AddIntMacro(m, MFD_HUGE_256MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014603#endif
14604#ifdef MFD_HUGE_512MB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014605 if (PyModule_AddIntMacro(m, MFD_HUGE_512MB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014606#endif
14607#ifdef MFD_HUGE_1GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014608 if (PyModule_AddIntMacro(m, MFD_HUGE_1GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014609#endif
14610#ifdef MFD_HUGE_2GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014611 if (PyModule_AddIntMacro(m, MFD_HUGE_2GB)) return -1;
Zackery Spytze70bfa952019-05-29 14:43:50 -060014612#endif
14613#ifdef MFD_HUGE_16GB
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014614 if (PyModule_AddIntMacro(m, MFD_HUGE_16GB)) return -1;
14615#endif
14616#endif
Victor Stinner9b1f4742016-09-06 16:18:52 -070014617
Giampaolo Rodola4a172cc2018-06-12 23:04:50 +020014618#if defined(__APPLE__)
14619 if (PyModule_AddIntConstant(m, "_COPYFILE_DATA", COPYFILE_DATA)) return -1;
14620#endif
14621
Steve Dower2438cdf2019-03-29 16:37:16 -070014622#ifdef MS_WINDOWS
14623 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DEFAULT_DIRS", LOAD_LIBRARY_SEARCH_DEFAULT_DIRS)) return -1;
14624 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_APPLICATION_DIR", LOAD_LIBRARY_SEARCH_APPLICATION_DIR)) return -1;
14625 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_SYSTEM32", LOAD_LIBRARY_SEARCH_SYSTEM32)) return -1;
14626 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_USER_DIRS", LOAD_LIBRARY_SEARCH_USER_DIRS)) return -1;
14627 if (PyModule_AddIntConstant(m, "_LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR", LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR)) return -1;
14628#endif
14629
Victor Stinner8c62be82010-05-06 00:08:46 +000014630 return 0;
Barry Warsaw4a342091996-12-19 23:50:02 +000014631}
14632
14633
Martin v. Löwis1a214512008-06-11 05:26:20 +000014634static struct PyModuleDef posixmodule = {
Victor Stinner8c62be82010-05-06 00:08:46 +000014635 PyModuleDef_HEAD_INIT,
14636 MODNAME,
14637 posix__doc__,
Eddie Elizondob3966632019-11-05 07:16:14 -080014638 sizeof(_posixstate),
Victor Stinner8c62be82010-05-06 00:08:46 +000014639 posix_methods,
14640 NULL,
Eddie Elizondob3966632019-11-05 07:16:14 -080014641 _posix_traverse,
14642 _posix_clear,
14643 _posix_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +000014644};
14645
14646
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014647static const char * const have_functions[] = {
Larry Hastings9cf065c2012-06-22 16:30:09 -070014648
14649#ifdef HAVE_FACCESSAT
14650 "HAVE_FACCESSAT",
14651#endif
14652
14653#ifdef HAVE_FCHDIR
14654 "HAVE_FCHDIR",
14655#endif
14656
14657#ifdef HAVE_FCHMOD
14658 "HAVE_FCHMOD",
14659#endif
14660
14661#ifdef HAVE_FCHMODAT
14662 "HAVE_FCHMODAT",
14663#endif
14664
14665#ifdef HAVE_FCHOWN
14666 "HAVE_FCHOWN",
14667#endif
14668
Larry Hastings00964ed2013-08-12 13:49:30 -040014669#ifdef HAVE_FCHOWNAT
14670 "HAVE_FCHOWNAT",
14671#endif
14672
Larry Hastings9cf065c2012-06-22 16:30:09 -070014673#ifdef HAVE_FEXECVE
14674 "HAVE_FEXECVE",
14675#endif
14676
14677#ifdef HAVE_FDOPENDIR
14678 "HAVE_FDOPENDIR",
14679#endif
14680
Georg Brandl306336b2012-06-24 12:55:33 +020014681#ifdef HAVE_FPATHCONF
14682 "HAVE_FPATHCONF",
14683#endif
14684
Larry Hastings9cf065c2012-06-22 16:30:09 -070014685#ifdef HAVE_FSTATAT
14686 "HAVE_FSTATAT",
14687#endif
14688
14689#ifdef HAVE_FSTATVFS
14690 "HAVE_FSTATVFS",
14691#endif
14692
Steve Dowerfe0a41a2015-03-20 19:50:46 -070014693#if defined HAVE_FTRUNCATE || defined MS_WINDOWS
Georg Brandl306336b2012-06-24 12:55:33 +020014694 "HAVE_FTRUNCATE",
14695#endif
14696
Larry Hastings9cf065c2012-06-22 16:30:09 -070014697#ifdef HAVE_FUTIMENS
14698 "HAVE_FUTIMENS",
14699#endif
14700
14701#ifdef HAVE_FUTIMES
14702 "HAVE_FUTIMES",
14703#endif
14704
14705#ifdef HAVE_FUTIMESAT
14706 "HAVE_FUTIMESAT",
14707#endif
14708
14709#ifdef HAVE_LINKAT
14710 "HAVE_LINKAT",
14711#endif
14712
14713#ifdef HAVE_LCHFLAGS
14714 "HAVE_LCHFLAGS",
14715#endif
14716
14717#ifdef HAVE_LCHMOD
14718 "HAVE_LCHMOD",
14719#endif
14720
14721#ifdef HAVE_LCHOWN
14722 "HAVE_LCHOWN",
14723#endif
14724
14725#ifdef HAVE_LSTAT
14726 "HAVE_LSTAT",
14727#endif
14728
14729#ifdef HAVE_LUTIMES
14730 "HAVE_LUTIMES",
14731#endif
14732
Zackery Spytz43fdbd22019-05-29 13:57:07 -060014733#ifdef HAVE_MEMFD_CREATE
14734 "HAVE_MEMFD_CREATE",
14735#endif
14736
Larry Hastings9cf065c2012-06-22 16:30:09 -070014737#ifdef HAVE_MKDIRAT
14738 "HAVE_MKDIRAT",
14739#endif
14740
14741#ifdef HAVE_MKFIFOAT
14742 "HAVE_MKFIFOAT",
14743#endif
14744
14745#ifdef HAVE_MKNODAT
14746 "HAVE_MKNODAT",
14747#endif
14748
14749#ifdef HAVE_OPENAT
14750 "HAVE_OPENAT",
14751#endif
14752
14753#ifdef HAVE_READLINKAT
14754 "HAVE_READLINKAT",
14755#endif
14756
14757#ifdef HAVE_RENAMEAT
14758 "HAVE_RENAMEAT",
14759#endif
14760
14761#ifdef HAVE_SYMLINKAT
14762 "HAVE_SYMLINKAT",
14763#endif
14764
14765#ifdef HAVE_UNLINKAT
14766 "HAVE_UNLINKAT",
14767#endif
14768
14769#ifdef HAVE_UTIMENSAT
14770 "HAVE_UTIMENSAT",
14771#endif
14772
14773#ifdef MS_WINDOWS
14774 "MS_WINDOWS",
14775#endif
14776
14777 NULL
14778};
14779
14780
Mark Hammondfe51c6d2002-08-02 02:27:13 +000014781PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +000014782INITFUNC(void)
Guido van Rossumb6775db1994-08-01 11:34:53 +000014783{
Victor Stinner8c62be82010-05-06 00:08:46 +000014784 PyObject *m, *v;
Larry Hastings9cf065c2012-06-22 16:30:09 -070014785 PyObject *list;
Serhiy Storchaka2d06e842015-12-25 19:53:18 +020014786 const char * const *trace;
Tim Peters5aa91602002-01-30 05:46:57 +000014787
Eddie Elizondob3966632019-11-05 07:16:14 -080014788 m = PyState_FindModule(&posixmodule);
14789 if (m != NULL) {
14790 Py_INCREF(m);
14791 return m;
14792 }
14793
Victor Stinner8c62be82010-05-06 00:08:46 +000014794 m = PyModule_Create(&posixmodule);
14795 if (m == NULL)
14796 return NULL;
Tim Peters5aa91602002-01-30 05:46:57 +000014797
Victor Stinner8c62be82010-05-06 00:08:46 +000014798 /* Initialize environ dictionary */
14799 v = convertenviron();
14800 Py_XINCREF(v);
14801 if (v == NULL || PyModule_AddObject(m, "environ", v) != 0)
14802 return NULL;
14803 Py_DECREF(v);
Fred Drakec9680921999-12-13 16:37:25 +000014804
Victor Stinner8c62be82010-05-06 00:08:46 +000014805 if (all_ins(m))
14806 return NULL;
Barry Warsaw4a342091996-12-19 23:50:02 +000014807
Victor Stinner8c62be82010-05-06 00:08:46 +000014808 if (setup_confname_tables(m))
14809 return NULL;
Fred Drakebec628d1999-12-15 18:31:10 +000014810
Victor Stinner8c62be82010-05-06 00:08:46 +000014811 Py_INCREF(PyExc_OSError);
14812 PyModule_AddObject(m, "error", PyExc_OSError);
Fred Drake762e2061999-08-26 17:23:54 +000014813
Ross Lagerwall7807c352011-03-17 20:20:30 +020014814#if defined(HAVE_WAITID) && !defined(__APPLE__)
Eddie Elizondob3966632019-11-05 07:16:14 -080014815 waitid_result_desc.name = MODNAME ".waitid_result";
14816 PyObject *WaitidResultType = (PyObject *)PyStructSequence_NewType(&waitid_result_desc);
14817 if (WaitidResultType == NULL) {
14818 return NULL;
14819 }
14820 Py_INCREF(WaitidResultType);
14821 PyModule_AddObject(m, "waitid_result", WaitidResultType);
Hai Shif707d942020-03-16 21:15:01 +080014822 get_posix_state(m)->WaitidResultType = WaitidResultType;
Ross Lagerwall7807c352011-03-17 20:20:30 +020014823#endif
14824
Eddie Elizondob3966632019-11-05 07:16:14 -080014825 stat_result_desc.name = "os.stat_result"; /* see issue #19209 */
14826 stat_result_desc.fields[7].name = PyStructSequence_UnnamedField;
14827 stat_result_desc.fields[8].name = PyStructSequence_UnnamedField;
14828 stat_result_desc.fields[9].name = PyStructSequence_UnnamedField;
14829 PyObject *StatResultType = (PyObject *)PyStructSequence_NewType(&stat_result_desc);
14830 if (StatResultType == NULL) {
14831 return NULL;
14832 }
14833 Py_INCREF(StatResultType);
14834 PyModule_AddObject(m, "stat_result", StatResultType);
Hai Shif707d942020-03-16 21:15:01 +080014835 get_posix_state(m)->StatResultType = StatResultType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014836 structseq_new = ((PyTypeObject *)StatResultType)->tp_new;
14837 ((PyTypeObject *)StatResultType)->tp_new = statresult_new;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014838
Eddie Elizondob3966632019-11-05 07:16:14 -080014839 statvfs_result_desc.name = "os.statvfs_result"; /* see issue #19209 */
14840 PyObject *StatVFSResultType = (PyObject *)PyStructSequence_NewType(&statvfs_result_desc);
14841 if (StatVFSResultType == NULL) {
14842 return NULL;
14843 }
14844 Py_INCREF(StatVFSResultType);
14845 PyModule_AddObject(m, "statvfs_result", StatVFSResultType);
Hai Shif707d942020-03-16 21:15:01 +080014846 get_posix_state(m)->StatVFSResultType = StatVFSResultType;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014847#ifdef NEED_TICKS_PER_SECOND
14848# if defined(HAVE_SYSCONF) && defined(_SC_CLK_TCK)
Eddie Elizondob3966632019-11-05 07:16:14 -080014849 ticks_per_second = sysconf(_SC_CLK_TCK);
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014850# elif defined(HZ)
Eddie Elizondob3966632019-11-05 07:16:14 -080014851 ticks_per_second = HZ;
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014852# else
Eddie Elizondob3966632019-11-05 07:16:14 -080014853 ticks_per_second = 60; /* magic fallback value; may be bogus */
Martin v. Löwis05bfe1f2008-12-29 18:21:47 +000014854# endif
14855#endif
Benjamin Peterson94b580d2011-08-02 17:30:04 -050014856
William Orr81574b82018-10-01 22:19:56 -070014857#if defined(HAVE_SCHED_SETPARAM) || defined(HAVE_SCHED_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDULER) || defined(POSIX_SPAWN_SETSCHEDPARAM)
Eddie Elizondob3966632019-11-05 07:16:14 -080014858 sched_param_desc.name = MODNAME ".sched_param";
14859 PyObject *SchedParamType = (PyObject *)PyStructSequence_NewType(&sched_param_desc);
14860 if (SchedParamType == NULL) {
14861 return NULL;
Victor Stinner8c62be82010-05-06 00:08:46 +000014862 }
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014863 Py_INCREF(SchedParamType);
Eddie Elizondob3966632019-11-05 07:16:14 -080014864 PyModule_AddObject(m, "sched_param", SchedParamType);
Hai Shif707d942020-03-16 21:15:01 +080014865 get_posix_state(m)->SchedParamType = SchedParamType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014866 ((PyTypeObject *)SchedParamType)->tp_new = os_sched_param;
Benjamin Petersone3298dd2011-08-02 18:40:46 -050014867#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000014868
Eddie Elizondob3966632019-11-05 07:16:14 -080014869 /* initialize TerminalSize_info */
14870 PyObject *TerminalSizeType = (PyObject *)PyStructSequence_NewType(&TerminalSize_desc);
14871 if (TerminalSizeType == NULL) {
14872 return NULL;
14873 }
14874 Py_INCREF(TerminalSizeType);
14875 PyModule_AddObject(m, "terminal_size", TerminalSizeType);
Hai Shif707d942020-03-16 21:15:01 +080014876 get_posix_state(m)->TerminalSizeType = TerminalSizeType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014877
14878 /* initialize scandir types */
14879 PyObject *ScandirIteratorType = PyType_FromSpec(&ScandirIteratorType_spec);
14880 if (ScandirIteratorType == NULL) {
14881 return NULL;
14882 }
Hai Shif707d942020-03-16 21:15:01 +080014883 get_posix_state(m)->ScandirIteratorType = ScandirIteratorType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014884
14885 PyObject *DirEntryType = PyType_FromSpec(&DirEntryType_spec);
14886 if (DirEntryType == NULL) {
14887 return NULL;
14888 }
14889 Py_INCREF(DirEntryType);
14890 PyModule_AddObject(m, "DirEntry", DirEntryType);
Hai Shif707d942020-03-16 21:15:01 +080014891 get_posix_state(m)->DirEntryType = DirEntryType;
Eddie Elizondob3966632019-11-05 07:16:14 -080014892
Larry Hastings605a62d2012-06-24 04:33:36 -070014893 times_result_desc.name = MODNAME ".times_result";
Eddie Elizondob3966632019-11-05 07:16:14 -080014894 PyObject *TimesResultType = (PyObject *)PyStructSequence_NewType(&times_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014895 if (TimesResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014896 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014897 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014898 Py_INCREF(TimesResultType);
14899 PyModule_AddObject(m, "times_result", TimesResultType);
Hai Shif707d942020-03-16 21:15:01 +080014900 get_posix_state(m)->TimesResultType = TimesResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014901
Eddie Elizondob3966632019-11-05 07:16:14 -080014902 PyTypeObject *UnameResultType = PyStructSequence_NewType(&uname_result_desc);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014903 if (UnameResultType == NULL) {
Victor Stinner1c8f0592013-07-22 22:24:54 +020014904 return NULL;
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014905 }
Eddie Elizondob3966632019-11-05 07:16:14 -080014906 Py_INCREF(UnameResultType);
Eddie Elizondo474eedf2018-11-13 04:09:31 -080014907 PyModule_AddObject(m, "uname_result", (PyObject *)UnameResultType);
Hai Shif707d942020-03-16 21:15:01 +080014908 get_posix_state(m)->UnameResultType = (PyObject *)UnameResultType;
Larry Hastings605a62d2012-06-24 04:33:36 -070014909
Thomas Wouters477c8d52006-05-27 19:21:47 +000014910#ifdef __APPLE__
Victor Stinner8c62be82010-05-06 00:08:46 +000014911 /*
14912 * Step 2 of weak-linking support on Mac OS X.
14913 *
14914 * The code below removes functions that are not available on the
14915 * currently active platform.
14916 *
14917 * This block allow one to use a python binary that was build on
Larry Hastings9cf065c2012-06-22 16:30:09 -070014918 * OSX 10.4 on OSX 10.3, without losing access to new APIs on
Victor Stinner8c62be82010-05-06 00:08:46 +000014919 * OSX 10.4.
14920 */
Thomas Wouters477c8d52006-05-27 19:21:47 +000014921#ifdef HAVE_FSTATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014922 if (fstatvfs == NULL) {
14923 if (PyObject_DelAttrString(m, "fstatvfs") == -1) {
14924 return NULL;
14925 }
14926 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014927#endif /* HAVE_FSTATVFS */
14928
14929#ifdef HAVE_STATVFS
Victor Stinner8c62be82010-05-06 00:08:46 +000014930 if (statvfs == NULL) {
14931 if (PyObject_DelAttrString(m, "statvfs") == -1) {
14932 return NULL;
14933 }
14934 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014935#endif /* HAVE_STATVFS */
14936
14937# ifdef HAVE_LCHOWN
Victor Stinner8c62be82010-05-06 00:08:46 +000014938 if (lchown == NULL) {
14939 if (PyObject_DelAttrString(m, "lchown") == -1) {
14940 return NULL;
14941 }
14942 }
Thomas Wouters477c8d52006-05-27 19:21:47 +000014943#endif /* HAVE_LCHOWN */
14944
14945
14946#endif /* __APPLE__ */
Antoine Pitroubcf2b592012-02-08 23:28:36 +010014947
Hai Shif707d942020-03-16 21:15:01 +080014948 if ((get_posix_state(m)->billion = PyLong_FromLong(1000000000)) == NULL)
Eddie Elizondob3966632019-11-05 07:16:14 -080014949 return NULL;
14950#if defined(HAVE_WAIT3) || defined(HAVE_WAIT4)
Hai Shif707d942020-03-16 21:15:01 +080014951 get_posix_state(m)->struct_rusage = PyUnicode_InternFromString("struct_rusage");
14952 if (get_posix_state(m)->struct_rusage == NULL)
Eddie Elizondob3966632019-11-05 07:16:14 -080014953 return NULL;
14954#endif
Hai Shif707d942020-03-16 21:15:01 +080014955 get_posix_state(m)->st_mode = PyUnicode_InternFromString("st_mode");
14956 if (get_posix_state(m)->st_mode == NULL)
Larry Hastings6fe20b32012-04-19 15:07:49 -070014957 return NULL;
14958
Larry Hastings9cf065c2012-06-22 16:30:09 -070014959 /* suppress "function not used" warnings */
14960 {
14961 int ignored;
14962 fd_specified("", -1);
14963 follow_symlinks_specified("", 1);
14964 dir_fd_and_follow_symlinks_invalid("chmod", DEFAULT_DIR_FD, 1);
14965 dir_fd_converter(Py_None, &ignored);
14966 dir_fd_unavailable(Py_None, &ignored);
14967 }
14968
14969 /*
14970 * provide list of locally available functions
14971 * so os.py can populate support_* lists
14972 */
14973 list = PyList_New(0);
14974 if (!list)
14975 return NULL;
14976 for (trace = have_functions; *trace; trace++) {
14977 PyObject *unicode = PyUnicode_DecodeASCII(*trace, strlen(*trace), NULL);
14978 if (!unicode)
14979 return NULL;
14980 if (PyList_Append(list, unicode))
14981 return NULL;
14982 Py_DECREF(unicode);
14983 }
14984 PyModule_AddObject(m, "_have_functions", list);
Ned Deilyeb3be662016-08-15 14:40:38 -040014985
Victor Stinner8c62be82010-05-06 00:08:46 +000014986 return m;
Guido van Rossumb6775db1994-08-01 11:34:53 +000014987}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000014988
14989#ifdef __cplusplus
14990}
14991#endif